Strange behavior of an LCD code

I am sending a string with exactly 32 characters to the Arduino. This is to be displayed on a 16x2 LCD.
Using the substring method I decompose the string into 2 parts of 16 characters each.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);

  String readString;

}

void loop() {
  String readString;
  while (Serial.available()) {
    delay(10);
    if (Serial.available() > 0) {
      char c = Serial.read();
      readString += c;
    }
  }
  if (readString.length() > 0) {
    lcd.setCursor(0, 0); 
    lcd.print(readString.substring(0, 16));
    lcd.setCursor(0, 1);   
    lcd.print(readString.substring(16, 32));
    delay(10);

    readString = "";
  }


}

As long as the data is being transmitted, the display is blank. When the sending program closes the com port, the last sent data suddenly appears correctly.

This code for a 16x1 display that I wrote has worked perfectly. I had used it as a template for the 16x2.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);

  String readString;

}

void loop() {
  String readString;
  while (Serial.available()) {
    delay(10);
    if (Serial.available() > 0) {
      char c = Serial.read();
      readString += c;
    }
  }
  if (readString.length() > 0) {
    lcd.setCursor(0, 0); //Linke Seite schreiben
    lcd.print(readString.substring(0, 8));
    lcd.setCursor(0, 1);    // Rechte Seite schreiben
    lcd.print(readString.substring(8, 16));
    delay(10);

    readString = "";
  }


}

Assuming that the serial data is being transmitted continuously, the delays you use ensure that no data is displayed until all is received.

Hi,
The code you say is for 16x1 is for 16x2;
All you have done is shorten the substring length.

I believe and observe that the monitor does not send data on each keypress, only after ENTER is hit.
So your program is functioning correctly.

Tom.... :slight_smile:

@ TomGeorge: The second code is for 16x1 (8 x 2) and works with the 16x1 display perfectly.

It was a timing problem. The timer of my VB program was too fast (200ms). I increased it to 300 ms and everything worked.
Thanks for your help.

Skino:
@ TomGeorge: The second code is for 16x1 (8 x 2) and works with the 16x1 display perfectly.

It was a timing problem. The timer of my VB program was too fast (200ms). I increased it to 300 ms and everything worked.
Thanks for your help.

What VB program?
Tom.... >:(

The program that sends the data to the Arduino via the serial interface.

Not sure of why you want to use String instead of a C string. Your code takes 4936 bytes. This code:

#include <LiquidCrystal.h>

//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  I like to leave 2 & 3 free for interrupts
LiquidCrystal lcd(12, 11, 7, 6, 5, 4); 

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  //  String readString;    // Why? Get rid of it.
}

void loop() {
  char readString[33];    // 1 extra for NULL
  int charsRead;

  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', readString, 32);
    readString[charsRead] = NULL;
    lcd.setCursor(0, 0);
    lcd.print(readString);
    readString[0] = NULL;
  }
}

takes 3236 bytes. I'm assuming that the VB program uses a newline character ('\n') to terminate the output stream. I think the second setCursor() isn't needed, because most LCD I've used wrap automatically to the next line.
This code is untested, as I don't have your VB program, nor do I have an LCD at the moment.

Skino:
The program that sends the data to the Arduino via the serial interface.

Yes, but why didn't you say so in your original post?
Tom.... :o

"...I am sending a string with exactly 32 characters to the Arduino..." :fearful:

Skino:
"...I am sending a string with exactly 32 characters to the Arduino..." :fearful:

But as you have found there is a difference between your VB program and a terminal program sending 32 characters.
How were we meant to test your code, if we didn't know you were using a customized program?

Good that you got it solved.
Please now edit the subject of this thread and ad [SOLVED] to it.

Thanks.. Tom... :slight_smile: