0
votes

I've been working to make an Arduino countdown timer. A 16x2 display is wired up, but now I just need to get the coding right.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 13, 11, 10, 9, 8);   //Pins used for display
signed short minutes, seconds;
char timeline[16];

void setup() {

  lcd.begin(16, 2); //Start cursor setting
  lcd.print("Time Remaining:"); //Top bar message


}

void loop() {

  lcd.setCursor(0, 1); 
  sprintf(timeline,"%0.2d:%0.2d secs", minutes, seconds);
  lcd.print(timeline);

  delay(1000);
  seconds--;

 if (minutes > 0 || seconds > 0); { // count down
  if (--seconds < 0) { 
   seconds = 59; 
    --minutes; 
    }
  }
}

My current issue is that it is counting up. Instead, I need it to count down starting at 45 minutes and 00 seconds and for the timer to stop once it has reached 00:00.

1
Are you sure this is the same code you're talking about? I can't see why it should be counting up. Seconds are probably counting down in sequence: 0 -1 -2 ... And it always skips '0' second so every minute will be 59s long.KIIV
I thought it would, but instead it starts at 00:00 and then moves into 00:-1, 00:-2, and so on.Jess
I'd start with if (minutes > 0 && seconds > 0) // count down. Also if (--seconds < 0) { seconds = 59; --minutes; }. And at last you should initialize minutes and seconds to something.KIIV
I'll give it a shot, thanksJess
It should be: if (minutes > 0 || seconds > 0)KIIV

1 Answers

0
votes

I need it to count down starting at 45 minutes and 00 seconds

I don't see you set this anywhere.

One more advice to make code easier to understand. Count in seconds only. 2 min. and 5 seconds are 125 seconds in total.
To output this convert it into minutes = totalseconds % 60, seconds = totalseconds / 60.