countdownarduino-unolcd

Arduino LCD 2 digits to 1 digit


I have a problem with showing numbers on the LCD screen in Tinkercad. The time is counting down from 60 seconds. But when it counts until 9-1 the number has shown as 90-10. Eg: LCD screen shows 20 at 2 seconds instead of 02 or 2 only. May I know how can I change it to only 1 digit or 09-01? Hope someone can help me with this. Thanks in advance.

Here my code for Arduino of LCD:

#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup()
{
  lcd.begin(16, 2); // Set up the number of columns and rows on the LCD.

  // Print a message to the LCD.
  lcd.print("Ambulance is approaching!");
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting
  // begins with 0):
  lcd.setCursor(0,1);
  
  // Print a message to the LCD.
  lcd.print("Time:");

  for (int seconds = 60; seconds > 0; --seconds){
    lcd.setCursor(6,1);
    lcd.print(seconds-1);
    delay(1000);
    
  }
}

Solution

  • This is because lcd.print only overwrites the necessary characters. So after displaying 11 it overwrites it with a 10 correctly. After that it overwrites the 1 with a 9 and the 0 stays because the new input is only one character long. You can clear the line by printing a string containing two spaces and then print the time.

      for (int seconds = 60; seconds > 0; --seconds){
        lcd.setCursor(6,1);
        lcd.print("  ");
        lcd.setCursor(6,1);
        lcd.print(seconds-1);
        delay(1000);
      }