Hi, Im writing the twitter feeder and it seem that part of my tweet got pushed down to the lower (16,1) of my LCD during the execution
I have No idea why this is happening. I have set my cursor only to (16,0) at the start of the program.
Here is the youtube video -
http://www.youtube.com/watch?v=q9Q1qYIJq7Y&feature=youtu.beHere is the section that I think might cause the problem
//Writing to LCD
int counterText = 0; // make new counter this is use to keep track of how much turn it has been run
int text_lenreverse = (text_len*-1)+29;
Serial.print("Text len reverse");
Serial.println(text_lenreverse);
//running the loop until the tex
for(int textPosi = 15; textPosi>text_lenreverse; textPosi--){
//sending the first 16 char
for(int thisChar = 0; thisChar < 16; thisChar++)
{
// exit loop and uses the next loop
lcd.setCursor(textPosi+thisChar,0);
lcd.print(text[thisChar]);
}
//print if and only if the text position is less than zero
if(textPosi < 0){
//loop printing the rest
for(int thisChar2 = 16; thisChar2 > 0;thisChar2--){
lcd.setCursor(thisChar2,0);
lcd.print(text[thisChar2+counterText]);
}
}
counterText++;
delay(150);
}//while(textPosition > 16);
Serial.println("Exit loop");
}
}
}
Here is the full code
/*
Twitter Client with Strings
*/
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192,168,1,20);
LiquidCrystal lcd(2, 3, 8, 5, 6, 7);
// initialize the library instance:
EthernetClient client;
const unsigned long requestInterval = 30000; // delay between requests
char serverName[] = "api.twitter.com"; // twitter URL
boolean requested; // whether you've made a request since connecting
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
String currentLine = ""; // string to hold the text from server
String tweet = ""; // string to hold the tweet
boolean readingTweet = false; // if you're currently reading the tweet
int lcd_start = 0; // Position of the text
int text_len = 0; // limit on LCD
int lcd_limit = 0; // the limit of lcd
int channel = 0; //Channel 0 = CNN 1 = Wired 2 = BBC
void setup() {
// reserve space for the strings:
currentLine.reserve(256);
tweet.reserve(150);
lcd.begin(16, 2);
lcd.print("Starting");
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// attempt a DHCP connection:
Serial.println("Attempting to get an IP address using DHCP:");
lcd.print(".");
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Serial.println("failed to get an IP address using DHCP, trying manually");
Ethernet.begin(mac, ip);
}
Serial.print("My address:");
Serial.println(Ethernet.localIP());
// connect to Twitter:
lcd.print(".");
connectToServer();
}
void loop()
{
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
}
// if the current line ends with <text>, it will
// be followed by the tweet:
if ( currentLine.endsWith("<text>")) {
// tweet is beginning. Clear the tweet string:
readingTweet = true;
tweet = "";
}
// if you're currently reading the bytes of a tweet,
// add them to the tweet String:
if (readingTweet) {
if (inChar != '<') {
tweet += inChar;
}
else {
// if you got a "<" character,
// you've reached the end of the tweet:
readingTweet = false;
Serial.println(tweet);
char text[tweet.length()];
tweet.toCharArray(text, tweet.length()+1);
text_len = (sizeof(text)/sizeof(char))-1;
lcd_limit = text_len - 16;
if(lcd_limit <= 0){
Serial.println("The text is less than 16 char");
lcd_limit = (sizeof(text)/sizeof(char))-1;
}
else{
Serial.print("the text lenght is ");
Serial.println(text_len);
}
//begin writing to LCD
// close the connection to the server:
Serial.println("Stopping Client");
client.stop();
lcd.clear();
if(channel == 1){
lcd.setCursor(0,1);
lcd.print("CNN");
}else if(channel == 2){
lcd.setCursor(0,1);
lcd.print("WIRED");
}else if(channel == 0){
lcd.setCursor(0,1);
lcd.print("BBCBNews");
}
lcd.setCursor(0,0);
Serial.print("Here is the text ");
Serial.println(text);
//Writing to LCD
int counterText = 0; // make new counter this is use to keep track of how much turn it has been run
int text_lenreverse = (text_len*-1)+29;
Serial.print("Text len reverse");
Serial.println(text_lenreverse);
//running the loop until the tex
for(int textPosi = 15; textPosi>text_lenreverse; textPosi--){
//sending the first 16 char
for(int thisChar = 0; thisChar < 16; thisChar++)
{
// exit loop and uses the next loop
lcd.setCursor(textPosi+thisChar,0);
lcd.print(text[thisChar]);
}
//print if and only if the text position is less than zero
if(textPosi < 0){
//loop printing the rest
for(int thisChar2 = 16; thisChar2 > 0;thisChar2--){
lcd.setCursor(thisChar2,0);
lcd.print(text[thisChar2+counterText]);
}
}
counterText++;
delay(150);
}//while(textPosition > 16);
Serial.println("Exit loop");
}
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}
void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Connecting");
if (client.connect(serverName, 80)) {
Serial.println("making HTTP request...");
lcd.clear();
lcd.setCursor(0,0);
if(channel == 0){
lcd.print("Requesting");
lcd.setCursor(0,1);
lcd.print("CNN");
client.println("GET /1/statuses/user_timeline.xml?screen_name=cnn&count=1 HTTP/1.1");
channel++;
}else if(channel == 1){
lcd.print("Requesting");
lcd.setCursor(0,1);
lcd.print("WIRED");
client.println("GET /1/statuses/user_timeline.xml?screen_name=wired&count=1 HTTP/1.1");
channel++;
}else if(channel == 2){
lcd.print("Requesting");
lcd.setCursor(0,1);
lcd.print("BBC Breaking News");
client.println("GET /1/statuses/user_timeline.xml?screen_name=BBCBreaking&count=1 HTTP/1.1");
channel=0;
}
client.println("HOST: api.twitter.com");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}