Greetings ladies and gents,
I am wondering if it is possible to limit the size of a string. I have a 4x20 LCD that I am displaying four different strings. Each string gets its own line. The strings are created by entering information in the serial monitor. If the string is over 20 characters then it wraps around to the next line. Then when the following line is printed it over prints the previous line. The idea is I would like to create a fixed size String or for that matter another solution that would do the same thing, to accept only a certain amount of data from the serial monitor and then toss the rest.
IE. "whats the plan?" would fit on the line
" Do we actually have a plan?" is too large to fit on the line and wraps around.
Here is the code that I have so far. This is a leaping off point for another project so at this point there is no real project I just want to know if/how this is possible. If I am doing it the hard way (most likely) feel free to direct me to a path of less resistance. Here is the code that I have so far:
#include <LiquidCrystal_I2C.h>
int Step=0;
String line1;
String line2;
String line3;
String line4;
LiquidCrystal_I2C lcd(0x27, 20, 4);//change the address, width and height here to match the LCD screen you are using.
void setup() {
Serial.begin(9600);
Serial.print("Serial to LCD display v1.0")
lcd.begin();
lcd.print("testing");
}
void loop() {
if (Step==0)
{
Serial.println("enter first line of 20 characters");
Step=1;
}
if (Step==1)
{
if (Serial.available() > 0)
{
String line1=Serial.readString();
lcd.clear();
lcd.print(line1);
Step=2;
}
}
if (Step==2)
{
Serial.println("enter second line of 20 characters");
Step=3;
}
if (Step==3)
{
if (Serial.available() > 0)
{
String line2=Serial.readString();
lcd.setCursor(0,1);
lcd.print(line2);
Step=4;
}
}
if (Step==4)
{
Serial.println("enter third line of 20 characters");
Step=5;
}
if (Step==5)
{
if (Serial.available() > 0)
{
String line3=Serial.readString();
lcd.setCursor(0,2);
lcd.print(line3);
Step=6;
}
}
if (Step==6)
{
Serial.println("enter fourth line of 20 characters");
Step=7;
}
if (Step==7)
{
if (Serial.available() > 0)
{
String line4=Serial.readString();
lcd.setCursor(0,3);
lcd.print(line4);
Step=0;
}
}
}//loop
Thanks for your attention
a machine tech