I was just optimizing my writing to my LCD2004A, and just wanted to share it in the event anyone wants it or searches for examples. EXAMPLE_ONE is just in the event it helps someone, I think EXAMPLE_TWO is a more universal soliton.
Original code was from an since closed topic.
//<EXAMPLE_ONE>
const String myNewLineText = "Hello World!";// Create your sting
lcd.setCursor(0,0);// Set cursor to 0,0, Line Zero Position Zero
lcd.print(myNewLineText);// Print your string to the LCD on line specified
// Specify the Line you want to Clear after new text,
// then the lenght of the new string so you can
// clear the line after your new text. If youw ant toclear the entire line
// just set ilength to 0/ZERO clearLCDLine(0, 0);.
clearLCDLine(0, myNewLineText.length());
void clearLCDLine(int iline, int ilength)
{
lcd.setCursor(ilength,iline);
for(int n = 0; n < 20; n++) // 20 indicates symbols in line. For 2x16 LCD write - 16
{
lcd.print(" ");
}
}
//</EXAMPLE_ONE>
//<EXAMPLE_TWO>
const String myNewLineText = "Hello World!";// Create your sting
clearLCDLineString(0, myNewLineText);// Right New String the Clear after it.
void clearLCDLineString(int iline, String myNewLineText)
{
// Writing the new string then immediately clear the rest of the line,
// a faster way to update a line.
int stringLenght = myNewLineText.length();
lcd.setCursor(0,iline);// Set Cursor to Position 0/ZERO
lcd.print(myNewLineText);// Print your string to the LCD on line specified
// Clear starting after the new text.
for(int n = stringLenght; n < 20; n++) // 20 indicates symbols in line. For 2x16 LCD write - 16
{
lcd.print(" ");
}
}
//</EXAMPLE_TWO>