lcd.clear() seems to clear the printing even after it has been called

I tried the following sketch trying to print the messages going between a computer and a gprs sheild on a lcd screen (16X2).
the sketch works great but for one problem. I want to clear the screen betwenn messages because of the residual past sentences at the end of the line. but using lcd.clear(); or trying to print 16 spaces makes the lcd stay blank all the time.

The sketch (The problematic commands are commented):

#include <NewSoftSerial.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(6,9,10,11,12,13);
NewSoftSerial mySerial(7, 8);
unsigned char readVal;
String topMessage; //Message to print
String buttomMessage; //Message to print
String newtopMessage; //Message Buffer
String newbuttomMessage; //Message Buffer
boolean printTopFlag = false;
boolean printButtomFlag = false;

void setup()
{
mySerial.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the GPRS baud rate
lcd.begin(16,2);
lcd.cursor(); //set cursor on;
}
void loop()
{
if(Serial.available()) // Messages from serial to GPRS sheild
{
readVal = (unsigned char)Serial.read();
if (((int)readVal != 13)&&((int)readVal != 10))
{
newtopMessage+=readVal; // Add to message received so far.
}
else // Until receiving CR or LF
{
printTopFlag = true;
}
mySerial.print(readVal);
}
else if(mySerial.available()) // Messages from GPRS sheild to serial
{
readVal = (unsigned char)mySerial.read();
if ((readVal != 13)&&(readVal != 10)) // Add to message received so far.
{
newbuttomMessage+=readVal;
}
else // Until receiving CR or LF
{
printButtomFlag = true;
}
Serial.print(readVal);
}
if (printTopFlag == true) //If finished receiving message from Serial.
{
// lcd.clear();
topMessage = newtopMessage;
printTopFlag = false;
newtopMessage = "";// Clear Buffer
/lcd.setCursor(0,1);
lcd.print(" ");
/
/* lcd.setCursor(0,0);
lcd.print(" "); /
lcd.setCursor(0,0); //Print the messages
lcd.print(topMessage);
lcd.setCursor(0,1);
lcd.print(buttomMessage);
}
if (printButtomFlag == true) //if finished recieving message from GPRS sheild
{
// lcd.clear();
buttomMessage = newbuttomMessage;
printButtomFlag = false;
newbuttomMessage = ""; //Clear buffer
/
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(" ");*/
lcd.setCursor(0,0); //Print the messages
lcd.print(topMessage);
lcd.setCursor(0,1);
lcd.print(buttomMessage);
}
}

What am I doing wrong?

The LCD screens that I have refresh very slowly, if you're clearing the display every single loop maybe you're not giving the screen enough time to "develop" the text that you do have printed before it clears again.

Rather than clearing and redrawing the entire line every time, try padding the right side of your text with spaces. So if you have a 10 character message put 6 spaces at the end of it. That will blank unused characters and prevent flicker on characters that don't change between refreshes.

Following extent's idea, what if you add delay (20) at the end of your loop() to slow down. You can't possibly read more than once a second on that screen anyway.

You'll probably want waaay more delay than that. I do a fixed 1/4 second minimum refresh time, That's about the limit of readability for a fast changing display that I found for my Sparkfun inverted LCD. My Newhaven one refreshes visibly quicker, but 4 refreshes a second still seems to be a fairly comfortable value.

Doing a full screen clear before writing will also introduce noticeable flicker even if you slow the refresh rate down, unless the entire message is changing every refresh cycle.

Finger slipped. delay(200) will do :slight_smile: I did use faster refresh rate on my serisl lcd panel firmware just in case a develpor wants to do typewriter effect.

I've seemed to solve it without using a delay (there is an inherit delay from the serial communication).
My mistake was closing a sentence both when receiving CR and LF and thus printing to the screen only the LF eventually.
Here is the corrected code for any one interested - Again this sketch is for communication between a GPRS sheild (Seeedstudio's v 1.0) and a computer while printing the commands to a 16X2 LCD.

//Serial Relay with LCD output - Arduino will patch a
//serial link between the computer and the GPRS Shield
//at 19200 bps 8-N-1
//Computer is connected to Hardware UART
//GPRS Shield is connected to the Software UART
//Based on an example in the Seeedstudio wiki.

#include <NewSoftSerial.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(6,9,10,11,12,13);
NewSoftSerial mySerial(7, 8);
unsigned char readVal;
String topMessage; //Message to print
String buttomMessage; //Message to print
String newtopMessage; //Message Buffer
String newbuttomMessage; //Message Buffer
boolean printTopFlag = false;
boolean printButtomFlag = false;

void setup()
{
mySerial.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the GPRS baud rate
lcd.begin(16,2);
lcd.cursor(); //set cursor on;
}
void loop()
{
if(Serial.available()) // Messages from serial to GPRS sheild
{
readVal = (unsigned char)Serial.read();
if (((int)readVal != 13)&&(readVal!=10))
{
newtopMessage+=readVal; // Add to message received so far.
}
else if (readVal!=13)// Until receiving CR or LF
{
printTopFlag = true;
}
mySerial.print(readVal);
}
else if(mySerial.available()) // Messages from GPRS sheild to serial
{
readVal = (unsigned char)mySerial.read();
if ((readVal != 13)&&(readVal!=10)) // Add to message received so far.
{
newbuttomMessage+=readVal;
}
else if (readVal!=13) // Until receiving CR or LF
{
printButtomFlag = true;
}
Serial.print(readVal);
}
if (printTopFlag == true) //If finished receiving message from Serial.
{
lcd.clear();
topMessage = newtopMessage;
printTopFlag = false;
newtopMessage = "";// Clear Buffer
lcd.setCursor(0,0); //Print the messages
lcd.print(topMessage);
lcd.setCursor(0,1);
lcd.print(buttomMessage);
}
if (printButtomFlag == true) //if finished recieving message from GPRS sheild
{
lcd.clear();
buttomMessage = newbuttomMessage;
printButtomFlag = false;
newbuttomMessage = ""; //Clear buffer
lcd.setCursor(0,0); //Print the messages
lcd.print(topMessage);
lcd.setCursor(0,1);
lcd.print(buttomMessage);
}
}

Thanks for the help.