Skip to main content
Alternative: prepend a space or a zero.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

instead of 9, it printed 90

No, it didn't: it printed “9”. However, it did not erase the previous value before printing “9”. Before printing the 9, the LCD had:

┌────────────────┐
│time left       │
│10              │
└────────────────┘
 ^cursor position

When you printed “9”, that character replaced the “1”, and you end up seeing “90”.

The fix, as pointed out by Juraj, is to add a space after printing the numbers.

An alternative is to prepend a space (or a zero), but only when the number to be displayed is less that 10:

int count = 20 - millis() / 1000;
if (count < 10)
    lcd.print(' ');  // alternatively: '0'
lcd.print(count);

instead of 9, it printed 90

No, it didn't: it printed “9”. However, it did not erase the previous value before printing “9”. Before printing the 9, the LCD had:

┌────────────────┐
│time left       │
│10              │
└────────────────┘
 ^cursor position

When you printed “9”, that character replaced the “1”, and you end up seeing “90”.

The fix, as pointed out by Juraj, is to add a space after printing the numbers.

instead of 9, it printed 90

No, it didn't: it printed “9”. However, it did not erase the previous value before printing “9”. Before printing the 9, the LCD had:

┌────────────────┐
│time left       │
│10              │
└────────────────┘
 ^cursor position

When you printed “9”, that character replaced the “1”, and you end up seeing “90”.

The fix, as pointed out by Juraj, is to add a space after printing the numbers.

An alternative is to prepend a space (or a zero), but only when the number to be displayed is less that 10:

int count = 20 - millis() / 1000;
if (count < 10)
    lcd.print(' ');  // alternatively: '0'
lcd.print(count);
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

instead of 9, it printed 90

No, it didn't: it printed “9”. However, it did not erase the previous value before printing “9”. Before printing the 9, the LCD had:

┌────────────────┐
│time left       │
│10              │
└────────────────┘
 ^cursor position

When you printed “9”, that character replaced the “1”, and you end up seeing “90”.

The fix, as pointed out by Juraj, is to add a space after printing the numbers.