LCD showing random characters???

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:

  1. Arduino Uno is connected to PC running a Processing program which handles the Twitter API stuff.
  2. Arduino sends a message over serial to the Processing program asking for the newest tweet in the timeline.
  3. 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:

Your problem may be related to the use of the lcd.clear() statement. This instruction takes the controller a long time to complete and this extra time is accounted for in the library. If your particular display is slower than normal the extra time provided may not be sufficient and your LCD controller could be missing some vital information. In any case the presence of this statement in loop() has been reported to degrade the appearance by causing flickering.

The LCD controller has the capability of automatically shifting the display (it's called autoscrolling in Arduinospeak). I haven't tried this but I think you would position the cursor at the right hand end of the line, implement autoscrolling, and then just start sending characters. Each new character should push the proceeding ones to the left until they fall off the display.

Don