I am trying to send a string over serial from my computer to my Arduino Uno. I can send data and have it print to my 16x2 LCD, but the lcd.clear() command doesn’t appear to work as I intended. Here is my short code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
char data[32] = {0};
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop() {
if (Serial) {
if (Serial.readBytesUntil(';', data, 32) != 0) {
lcd.clear();
lcd.write(data);
Serial.flushRX();
}
}
}
I know the wiring for the LCD is right, because I tried a simple sketch with the same pin initialization that simply displayed a string, waited a second, cleared the LCD, waited a second, then restarted the loop. This worked exactly as intended.
But in my code, the screen never clears, even though the data variable is written. So if I send “Hello, world!;” from the Serial Monitor, you see “Hello, world!” on the screen. But if I send “Bye!;” after that, what shows on the screen is “Bye!o, world!”. So clearly lcd.write() is being called, while lcd.clear() is being skipped over.
If anyone is wondering what Serial.flushRX() is, I just did as described here:
https://forum.sparkfun.com/viewtopic.php?f=32&t=32715#p162858
If I don’t do that, the first character, and one character after the last, is 4 horizontal lines with spaces between each, so obviously I need to clear the buffer. Even without Serial.flushRX() it still doesn’t clear the screen, so that’s not the problem.
The only thing I can guess is the problem, is flushRX() isn’t actually clearing the buffer, and so the old value remains, and is being partially overwritten with lcd.readBytesUntil().
Does anyone know why lcd.clear() won’t work for me here?
Thanks!
Some of you may notice I post an awful lot of threads here… I really do try to get the answer on my own, it’s just all my life I’ve had problems understanding things, so often I miss something on my own.