Hi,
I'm a bit of a noob to the Arduino and microcontrollers in general, but I decided to try and write a program which would show my tweets on a 16x2 LCD and scroll them across the part of the screen I am printing to. The way the setup works is as follows:
- Arduino Uno is connected to PC running a Processing program which handles the Twitter API stuff.
- Arduino sends a message over serial to the Processing program asking for the newest tweet in the timeline.
- The Processing program sends the tweet back to the Arduino which then scrolls it across the screen.
My problem is that at random times, the LCD shows random characters across the whole screen.
Here is my Arduino code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
String str = "";
int index = 0;
long prevTime = 0;
String readSerialString()
{
String in = "";
if (Serial.available() > 0)
{
while (Serial.available() > 0) {
in+=(char)Serial.read();
delay(5);
}
in.replace("\n", " ");
return in;
} else {
return "";
}
}
void setup()
{
Serial.begin(9600);
analogWrite(10, 128);
lcd.begin(16,2);
//Send command, seperator (;) and number of tweets I want (1) and terminator (/)
Serial.print("getTimeline;1/");
while (Serial.available() == 0);
//Read the reply to the "str" variable
str = readSerialString();
}
void loop()
{
//Update screen and string every 300 milliseconds
unsigned long cur = millis();
if (cur - prevTime > 300)
{
prevTime = cur;
String toPrint = "";
if (str.length() <= 16)
{
toPrint = str;
} else {
toPrint = str.substring(index, index+16);
}
if(index < str.length()) index++;
else index = 0;
lcd.clear();
lcd.print(toPrint);
}
}
EDIT: Here's a video: