Countdown Timer

Hello,

I am trying to design a 60 second count down timer. I want the timer to start when push button is pressed. When the timer is done I want a idle message until the same push button is pressed.

I have the timer counting down but when it reaches 0 it jumps to a trigger to display “Game Over”. Currently I have it programmed to “loop()” after a delay because I am just trying to get it to reset. When it loops all it does is display the “Count Down Timer” line but it doesn’t start timing or counting down

I am ignoring the push button part right now and focusing on getting the timer to self reset after a delay

#include <LiquidCrystal.h>
// Goal; 60 second count down timer, when reaches 0 display “Game over”
// Continue displaying “Game Over” until start button is pressed to reset

int hours = 0; // start hours
int minutes = 0; //start min
int seconds = 60; //start seconds

//#define startbutton 7 // start button on pin 7

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //<–removed- Different LCD manufacture

void setup() {
//pinMode (startbutton, INPUT_PULLUP); //
// digitalWrite (startbutton, LOW);
}

void loop() {
lcd.begin(16, 2);
lcd.print("Count Down Timer ");

// lcd.scrollDisplayLeft();
// wait a bit:
delay(2000);

while (hours > 0 || minutes > 0 || seconds >= 0) {

lcd.setCursor(4, 2);

(hours < 10) ? lcd.print(“0”) : NULL;
lcd.print(hours);
lcd.print(":");
(minutes < 10) ? lcd.print(“0”) : NULL;
lcd.print(minutes);
lcd.print(":");
(seconds < 10) ? lcd.print(“0”) : NULL;
lcd.print(seconds);
lcd.display();
stepDown();
delay(1000);
}
}

void stepDown() {
if (seconds > 0) {
seconds -= 1;
} else {
if (minutes > 0) {
seconds = 59;
minutes -= 1;
} else {
if (hours > 0) {
seconds = 59;
minutes = 59;
hours -= 1;
} else {
trigger();
}
}
}
}

void trigger() {
lcd.clear(); // clears the screen and buffer
lcd.setCursor(0, 1); // set timer position on lcd for end.

lcd.println("GAME OVER ");
delay(1000);
loop(); // restart timer

//if (startbutton == HIGH) {
//// if start button pressed, reset timer
//loop();
}
//}

Consider looking at the example sketch Blink Without Delay.

 loop(); // restart timer

No. You shouldn't call loop. You let loop end and it runs over again all on its own. If you call it like that before it is don'e you've created a recursion. That's a recipe for a crash if you don't really really know what you are doing and you obviously do not here.

If you want the timer to start over again at 60 seconds then at some point you're going to have to set seconds back to 60. As it is it is repeating the countdown but seconds is already 0 so there's nothing to count.

You should also create a new variable, totalSecondsToCountDown, and set it to the proper value when the time to count down is known. Then, instead of having three variables to check to see if time is up, you only have one. Your stepDown() function then needs to simply decrement totalSecondsToCountDown.

You might wish to have a function that converts totalSecondsToCountDown to hours, minutes, and seconds, or you may not. If you do, though, such a function is trivial to write.