I recently purchased a very simple, Hitachi HD44780 compatible LCD screen for an Arduino project. It’s a 20x4 I2C display, which apparently is constructed as a combination of 2 two-line display controllers. This wouldn’t really matter except for the fact that when printing strings longer than a single line, they are displayed in a 1324 matter. So if you set your cursor on the first line and print a string 80 characters long, things get weird.
lcd.setCursor(0,0)
lcd.print("11111111111111111111222222222222222222223333333333333333333344444444444444444444");
The display shows the lines in a incorrect order. Here’s what you would see on the display:
11111111111111111111
33333333333333333333
22222222222222222222
44444444444444444444
Fortunately this is easily resolved by writing a custom print function that moves the cursor to the correct line before printing. Here’s the function was using in my project. The only difference is that this version does not remove leading whitespace.
void print(String s){
lcd.clear();
int currentLine = 0;
int increment = LCD_ROWS;
for (int start = 0; start < s.length() ; start += increment){
if(currentLine < 4){
String line;
if(start + LCD_ROWS > s.length()){
line = s.substring(start);
}else{
line = s.substring(start, start + LCD_ROWS);
}
lcd.setCursor(0,currentLine++);
lcd.print(line);
}
}
delay(100);
}
I’m using this print function in combination with the New LiquidCrystal library by F. Malpartida.