Hardware Reference
In-Depth Information
loop() checks what state it's in
using a switch statement. When it
has finished what it needs to do in each
state, it increments the state variable
and moves to the next state on the
next time through the loop() . If any
of the later states don't succeed (for
example, if it can't authenticate or read
from the last tag it saw), it drops back
to the first state, looking for a tag.
8
void loop() {
switch(state) {
case 0: // get tag
tag = Rfid.selectTag();
if (tag != 0) {
// you have a tag, so print it:
Serial.println(tag, HEX);
state++; // go to the next state
}
break;
case 1: // read block
if (Rfid.authenticate(addressBlock)) {
Serial.print(F("authenticated "));
// read the tag for the twitter handle:
Rfid.readBlock(addressBlock);
twitterHandle = Rfid.getString();
// show the handle:
lcd.clear(); // clear previous stuff
lcd.setCursor(0,0); // move the cursor to the beginning
lcd.print(twitterHandle); // tweet handle on the top line
Serial.println(twitterHandle);
state++; // go to the next state
}
else state = 0; // go back to first state
break;
case 2: //connect to server
// if this is a new tag, or if the request delay
// has passed since the last time you made an HTTP request:
if (tag != lastTag ||
millis() - lastRequestTime > requestDelay) {
// attempt to connect:
if (connectToServer()) {
state++; // go to the next state
}
else state = 0; // go back to first state
}
else state = 0; // go back to first state
lastTag = tag;
break;
case 3: // read response
tweetLength = readResponse();
state = 0; // go back to first state
break;
}
Once it has finished checking the
state, loop() updates the LCD display.
It prints the Twitter handle on the top
line, and it scrolls the latest tweet on
the bottom line, moving forward every
scrollDelay milliseconds.
// if you haven't moved the LCD recently:
if (tweetLength > 0 && millis() - lastScrollTime > scrollDelay) {
// advance the LCD:
scrollLongString(cursorPosition);
// increment the LCD cursor position:
if (cursorPosition < tweetLength) {
cursorPosition++;
ยป
 
Search WWH ::




Custom Search