Serial input to LCD problem

I’m using a standard HDD4780 (or something like that, i forget the exact model) compliant LCD for this project, and it does work with simpler programs. My problem is that it seems to overwrite itself sometimes. I assume its my code, but I can’t be sure. Here’s the code:

#include <LCD4Bit.h> 

LCD4Bit lcd = LCD4Bit(2); 


int x = 0;
int i =0;
int button = 13;
char g[53]="";
char u[25]="";
char ut[25]="";
char l[25]="";
char lt[25]="";
char b[25]="                       ";
int scrolled=0;


void setup() 
{
  lcd.init();
  Serial.begin(19200);
}

void loop() 
{
  // Serial data waiting?
  if (Serial.available())
  {
    delay(100);
    
    // Read in the serial data to g
    while(Serial.available()>0)
    {
      g[x]=Serial.read();
      x++;
    }
    x=0;
    char ch = g[x];
    
    // Begin filling the upper temp, until the % character
    while (g[x] != '%')
    {
      ut[x-1]=g[x];
      x++;
    }
    
    // Was that actually wanted data?
    if (g[0] != '

This works in the way that data can be sent, 2 lines at a time in 1 string. The first character has to be $ to get first line data, the % character denotes the end of the first line, and ^ is the endline character. Each line is held to 25 characters, while the input buffer (g) is 53, to account for 50 characters and 3 delimiters. The b string is for blanking a line when I want to write new data, in case the new data is shorter than the old (it overwrites, so I have to make sure nothing is left over to screw up what i see).

The problem is this: when I first send data, it all displays, but scrolls. That i can live with, but the second (and ever after) data send does not display correctly. The first line seems to get blanked, first 8 characters then 16 then probably 32, but I dont go that high anyway. It is supposed to be able to write a single line (top if send $(text)%^, bottom is sent %(text)^), but wont do it. The bottom always writes correctly, the top is giving me real problems. Any ideas?

[edit]
Many of the delays are probably not needed, I was just trying before to see if it was overwriting itself on a timing problem or something.
[/edit])
   {
     // If not, clear the upper temp
     for(int i = 0; i < 25; i++)
     {
       ut[i]=’ ‘;
     }
   }
   
   // Fill the rest to the lower temp until ^ character
   while(g[x+1] != ‘^’)
   {
     x++;
     lt[i]=g;
     i++;
   }
   x=0;
   i=0;
   
   // Clear g
   for(int i = 0; i < 53; i++)
   {
     g[i]=’ ';
   }
 
   // Upper temp not empty?
   if (ut[0] != ’ ‘)
   {
     // Clear upper line
     lcd.cursorTo(1,0);
     delay(1);
     lcd.printIn(b);
     delay(10);
     // Print new data
     lcd.cursorTo(1,0);
     delay(1);
     lcd.printIn(ut);
     delay(10);
     // Save temp
     for(int i = 0; i < 25; i++)
     {
       u[i]=ut[i];
       ut[i]=’ ';
     }
   }
   
   // Lower temp not empty?
   if (lt[0] != ’ ‘)
   {
     // Clear lower line
     lcd.cursorTo(2,0);
     delay(1);
     lcd.printIn(b);
     delay(10);
     // Print new data
     lcd.cursorTo(2,0);
     delay(1);
     lcd.printIn(lt);
     delay(10);
     // Save temp
     for(int i = 0; i < 25; i++)
     {
       l[i]=lt[i];
       lt[i]=’ ';
     }
   }
 }
 
 // Otherwise, check if we should be scrolling
 while(!(Serial.available() > 0))
 {
   delay(1500);
   // Scroll if the string is off the screen
   if((u[16] != ’ ')||(u[17] != ’ ')||(l[16] != ’ ')||(l[17] != ’ '))
   {
     lcd.leftScroll(10,750);
     scrolled=1;
   }
   // If scrolled, redraw the text
   if (scrolled == 1)
   {
     lcd.clear();
     lcd.cursorTo(1,0);
     lcd.printIn(u);
     delay(1);
     lcd.cursorTo(2,0);
     lcd.printIn(l);
     scrolled=0;
   }
 }
}


This works in the way that data can be sent, 2 lines at a time in 1 string. The first character has to be $ to get first line data, the % character denotes the end of the first line, and ^ is the endline character. Each line is held to 25 characters, while the input buffer (g) is 53, to account for 50 characters and 3 delimiters. The b string is for blanking a line when I want to write new data, in case the new data is shorter than the old (it overwrites, so I have to make sure nothing is left over to screw up what i see).

The problem is this: when I first send data, it all displays, but scrolls. That i can live with, but the second (and ever after) data send does not display correctly. The first line seems to get blanked, first 8 characters then 16 then probably 32, but I dont go that high anyway. It is supposed to be able to write a single line (top if send $(text)%^, bottom is sent %(text)^), but wont do it. The bottom always writes correctly, the top is giving me real problems. Any ideas?

[edit]
Many of the delays are probably not needed, I was just trying before to see if it was overwriting itself on a timing problem or something.
[/edit]

My problem is that it seems to overwrite itself sometimes

try inserting lcd.clear();

in some cases that might help :slight_smile:

Try This, I Think I Fixed It For You :stuck_out_tongue:

#include <LCD4Bit.h>

LCD4Bit lcd = LCD4Bit(2);


int x = 0;
int i =0;
int button = 13;
char g[53]="";
char u[25]="";
char ut[25]="";
char l[25]="";
char lt[25]="";
char b[25]="                       ";
int scrolled=0;


void setup()
{
  lcd.init();
  Serial.begin(19200);
}

void loop()
{
lcd.clear();
  // Serial data waiting?
  if (Serial.available())
  {
    delay(100);
    
    // Read in the serial data to g
    while(Serial.available()>0)
    {
      g[x]=Serial.read();
      x++;
    }
    x=0;
    char ch = g[x];
    
    // Begin filling the upper temp, until the % character
    while (g[x] != '%')
    {
      ut[x-1]=g[x];
      x++;
    }
    
    // Was that actually wanted data?
    if (g[0] != '

)
   {
     // If not, clear the upper temp
     for(int i = 0; i < 25; i++)
     {
       ut[i]=’ ‘;
     }
   }
   
   // Fill the rest to the lower temp until ^ character
   while(g[x+1] != ‘^’)
   {
     x++;
     lt[i]=g;
     i++;
   }
   x=0;
   i=0;
   
   // Clear g
   for(int i = 0; i < 53; i++)
   {
     g[i]=’ ';
   }
 
   // Upper temp not empty?
   if (ut[0] != ’ ‘)
   {
     // Clear upper line
     lcd.cursorTo(1,0);
     delay(1);
     lcd.printIn(b);
     delay(10);
     // Print new data
     lcd.cursorTo(1,0);
     delay(1);
     lcd.printIn(ut);
     delay(10);
     // Save temp
     for(int i = 0; i < 25; i++)
     {
       u[i]=ut[i];
       ut[i]=’ ';
     }
   }
   
   // Lower temp not empty?
   if (lt[0] != ’ ‘)
   {
     // Clear lower line
     lcd.cursorTo(2,0);
     delay(1);
     lcd.printIn(b);
     delay(10);
     // Print new data
     lcd.cursorTo(2,0);
     delay(1);
     lcd.printIn(lt);
     delay(10);
     // Save temp
     for(int i = 0; i < 25; i++)
     {
       l[i]=lt[i];
       lt[i]=’ ';
     }
   }
 }
 
 // Otherwise, check if we should be scrolling
 while(!(Serial.available() > 0))
 {
lcd.clear();
   delay(1500);
   // Scroll if the string is off the screen
   if((u[16] != ’ ')||(u[17] != ’ ')||(l[16] != ’ ')||(l[17] != ’ '))
   {
     lcd.leftScroll(10,750);
     scrolled=1;
   }
   // If scrolled, redraw the text
   if (scrolled == 1)
   {
     lcd.clear();
     lcd.cursorTo(1,0);
     lcd.printIn(u);
     delay(1);
     lcd.cursorTo(2,0);
     lcd.printIn(l);
     scrolled=0;
   }
 }
}

and if that dont work, tell me what you mean by (overwriting its self)

Thanks, i tried it but not quite working how i want/how it should. I’m not sure if you did anything else but the clears, but I’m using the code you reposted without the clear at the beginning of the loop{} and the beginning of the while(!Serial) loop, just to make sure. Anyway, what happens is after the third time sending data, the first 3 characters of the top line are blanked. Say i send $Hello%World^ first. It shows up, on both lines as expected, but scrolls. That i can live with. the 2nd thing i send is TOP%BOTTOM^. That works fine, both lines no scrolling. Even if I sent %Hello bottom^, it does as expected and only modifies the bottom line. But now, the third send has issues. Say i send $Helloooooo%Again!^, all that prints is loooooo Again!, but on two lines like how it should. Thereafter, the first line is missing 3 characters (blanked). Also, I can’t overwrite the top line without clearing the bottom, but I can overwrite the bottom without clearing the top. Thanks for the try though :-/

try printing" hello world"

maby you have to put 3 or 4 or 5 spaces before your print :slight_smile: