LCD Library with 16x2 and new lines

Hey everyone,

after googling for an hour or two, I couldn't find anything of use, so I'll just ask here.

Does anyone have an easy way of reading Serial.read() and parsing that so that anything above 16 chars goes to the second line? Perhaps storing the whole string in a variable, then removing the first 16 chars and printing the remaining chars on a new line.

I tried to parse it myself, but couldn't find an easy(ish) way of accomplishing it, so just wondering if anyone has any pointers, ideas or better yet: snippets. :-)

Something like this?

int numChars = 0;

//Fill top line
while ( numChars < 16 )
{
      while ( !Serial.available() ); //Wait for char
      LCD.print(Serial.read());
      numChars++;
}

LCD.cursorTo(0,1); //Move to second line

//Fill bottom line
numChars = 0;
while ( numChars < 16 )
{
      while ( !Serial.available() ); //Wait for char
      LCD.print(Serial.read());
      numChars++;
}

I’m not sure on the specifics of the LCD library you’re using, but the rest should work.

thats what I was thinking.

btw LiquidCrystal indexs rows starting at 0 so second line is:

lcd.setCursor(0,1);

Yeah, it's been a while since I used the LiquidCrystal library and I couldn't be bothered looking it up. (Fixed).

Thanks a ton for the suggestion!

This is almost what I'm trying to achieve, but instead every time I output new data over the serial, I'm executing lcd.clear(); while the code you posted outputs new data at the end of the old.

I also noticed that after it reaches the end, it does "clear", but it's sort of "stuck" on the second line.

So to recap: I'm trying to create it so that every time I post something over the serial, it'll get printed on the LCD, while if that data's longer than 16 characters it'll go to a new line. But if new data is posted, it should clear the screen.

I'll keep tweaking it, I'm propably just doing it wrong anyway. ;)

(PS. I'm also trying to implement a "clear command", so if I send for example "clear" and nothing else, it would run lcd.clear(), though when I tried this it did work but somehow the cursor went to -1 instead of 0)

Ripped and modded from SerialDisplay:

void loop()
{
  int charcount;
  boolean secondline;
  if (Serial.available()) {
    delay(200);
    lcd.clear();
    charcount = 0;
    secondline = false;
    while (Serial.available() > 0) {
      if (charcount > 15 && secondline == false ) {
        lcd.setCursor(0,1);
        secondline = true;
      }
      lcd.write(Serial.read());
      charcount++;
    }
  }
}

Edit: to just clear the screen, just send nothing a space.

Yes!

This is exactly what I was going for!

Thank you very, very much guys. :slight_smile:

I've tried to use the code you supplied to also implement some sort of parsing of commands, so if for example I send "/KEYWORD" it would automatically know that it shouldn't print anything but instead look for that "KEYWORD" and the action defined for it.

The main problem I have is that when I'm reading the serial input for "keywords" I lose the first character when I print the text on the LCD.

Any ideas on this? :)

EDIT: What would actually be even better, would be if even printing on the LCD would require something like "/TEXT:Hello"

your turn to post some code mate. What do you have so far?

I would think you could use:

if ( Serial.read() == "/" ) { // Serial.read the command // command handling code } else { // print as normal }

Of course that means no printing "/"s unless you have error handling