Hardware Reference
In-Depth Information
Continued from opposite page.
}
else {
cursorPosition = 0;
}
// note the last time you moved the LCD:
lastScrollTime = millis();
}
8
Finally, it updates scrollDelay by
mapping the input of the potentiom-
eter on pin A1.
// update the speed of scrolling from the second potentiometer:
int sensorReading = analogRead(potInput);
// map to a scrolling delay of 100 - 300 ms:
scrollDelay = map(sensorReading, 0, 1023, 100, 300);
}
The scrollLongString() method
takes a 16-character substring of
the long tweet and displays it on the
LCD display. It pads the beginning and
end of the tweet with enough charac-
ters so that it scrolls all the way on and
off the screen.
8
// this method takes a substring of the long
// tweet string to display on the screen
void scrollLongString(int startPos) {
String shortString = ""; // the string to display on the screen
// make sure there's enough of the long string left:
if (startPos < tweetLength - screenWidth) {
// take a 16-character substring:
shortString = tweet.substring(startPos, startPos + screenWidth);
}
// refresh the LCD:
lcd.clear(); // clear previous stuff
lcd.setCursor(0,0); // move the cursor to beginning of the top line
lcd.print(twitterHandle); // tweet handle on the top line
lcd.setCursor(0,1); // move cursor to beginning of the bottom line
lcd.print(shortString); // tweet, scrolling, on the bottom
}
The connectToServer() method is
very similar to the connect method
in the air-quality client in Chapter 4. It
connects to the server and makes an
HTTP GET request. In this case, it's not
asking for an HTML page, but for an
XML version of the tweet that you can
get from http://api.twitter.com . Twitter,
like many sites, makes it possible to
get both a human-readable form of
their site and a machine-readable one,
delivered in XML.
8
// this method connects to the server
// and makes an HTTP request:
boolean connectToServer() {
// note the time of this connect attempt:
lastRequestTime = millis();
// attempt to connect:
Serial.println(F("connecting to server"));
if (client.connect(server, 80)) {
Serial.println(F("making HTTP request"));
// make HTTP GET request:
client.print(F("GET /1/statuses/user_timeline.xml?screen_name="));
client.print(twitterHandle);
client.println(F(" HTTP/1.1"));
client.println(F("Host:api.twitter.com"));
client.println();
return true;
}
ยป
 
Search WWH ::




Custom Search