I have an LCD display 16x2 set up with I2C. I want to check if the input string from serial is longer than 16 characters, so I can scroll it and if it is shorter just show the string on the LCD. I am stuck on the following issue, if the string is short, I get a double flag on the end of the string, or if I have printed a longer string, I can't clear the line completely and I have leftovers on the end... Here is my code and photos of my problem
#include <LiquidCrystal_I2C.h>
// LiquidCrystal object construction
LiquidCrystal_I2C lcd (0x3F, 16, 2);
// variables for timing proccess
unsigned long startTime;
unsigned long currentTime;
unsigned long delayTime = 1000;
String input; // input String from Serial stored in this var
boolean StringProcessing = false; // indication of a String is printed on lcd
boolean bigString = false; // indication of a long or short String
// variables used during longStringProcessing to print a String
// 16 characters at a time (rolling text message)
int i = 0;
int k = 16;
void setup() {
Serial.begin(115200); // initialize Serial for debuggin
lcd.init(); // initiate lcd
lcd.clear(); // clear lcd
lcd.backlight(); // turn on the backlight
// print on the second line a string
lcd.setCursor(0,1);
lcd.print("testing");
}
void loop() {
if (Serial.available()) {
serialEvent();
}
if (StringProcessing) {
currentTime = millis();
if (bigString) {
if ((currentTime - startTime) >= delayTime) {
Serial.println(input.substring(i,k));
lcd.setCursor(0, 0);
lcd.print("");
lcd.setCursor(0, 0);
lcd.print(input.substring(i,k));
i++;
k++;
startTime = currentTime;
if (k >= input.length()) {
StringProcessing = false;
bigString = false;
}
}
} else {
Serial.println(input);
Serial.println("short string");
Serial.println(input.length());
lcd.setCursor(0, 0);
lcd.print("");
lcd.setCursor(0, 0);
lcd.print(input);
StringProcessing = false;
}
}
}
void lengthCheck(String aString) {
if (aString.length() > 16) {
bigString = true;
startTime = currentTime;
}
}
void serialEvent() {
input = Serial.readString();
lengthCheck(input);
Serial.println(bigString);
Serial.println("Starting now");
StringProcessing = true;
}
Function lengthCheck(String aString) doesn't reset 'bigString'. Perhaps it should live up to it's name, rather than relying on the mainline code to clear the flag when it's done?
The trailing character is probably a NL or CR character. Check your line endings in the IDE serial monitor.
This code...
lcd.print("");
...does not clear the line. To clear the line print enough spaces to fill an entire line (16);
Edit:
4. Nothing ever resets i or k. Suggest resetting at the same time you get new serial input.
+Karma for:
a) Using code tags on your first post
b) Using millis() for timing on your first sketch (and not delay() like everyone else)
Function lengthCheck(String aString) doesn't reset 'bigString'. Perhaps it should live up to it's name, rather than relying on the mainline code to clear the flag when it's done?
You do have a point, it's just for experimenting around, since all this is part of a bigger project
pcbbc:
2. The trailing character is probably a NL or CR character. Check your line endings in the IDE serial monitor.
3. This code...
lcd.print("");
...does not clear the line. To clear the line print enough spaces to fill an entire line (16);
You are correct, this looks like a /n on Serial, do you have any idea on how to delete it? Would a conversation to char array solve my problem? Filling the remaining spots with " "? I'll try to mess around with that one...
pcbbc:
Edit:
4. Nothing ever resets i or k. Suggest resetting at the same time you get new serial input.
panos_gkrigkas:
You do have a point, it's just for experimenting around, since all this is part of a bigger project.
If part of a bigger project even more reason to do things in consistent places.
You are correct, this looks like a /n on Serial, do you have any idea on how to delete it?
The easiest way would be not to send it! Check your line endings in the IDE serial monitor.
Would a conversation to char array solve my problem?
No. You'd still have a line ending.
If you insist on sending it, search through the string and remove it, or see the serial input basics for how to code your own ReadString routines.