I tried to modify the basic "hello-world" sketch for the LCD that is a part of the examples section in the IDE. I intended to make a countdown timer using the LCD.
The original code simply prints "Hello World" in the first line, and then makes use of the millis()
function to print the time elapsed in the next line.
So I tried a simple modification to make it count down from 20. Instead of printing millis()/1000
, I printed 20-millis()/1000
, and also added a while()
to only print till millis()<=20,000
. (so that it does not go beyond 00)
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("time left");
}
void loop() {
while(millis()<=20000){
lcd.setCursor(0, 1);
lcd.print(20- millis() / 1000);
}
}
It seemed to work till 10
. but instead of 9
, it printed 90
, and likewise 80,70..
for 8,7...
etc till 00
.
How can I get rid of these extra zeros?
lcd.print(' ');