Hello newbie here, I am working on a countdown timer that starts when you press the button attached. The code works without the button, but I’ve been struggling to get the button to start the timer, now it just removes 2 seconds and stops again. It’s displayed on an lcd, which I’ve gotten to work so far. i think the problem is within the code since the pushbutton stops once no longer pressed.
If anyone could help I’d really appreciate it, I’ve been fighting with it for a couple of days now. thanks! Code below:
//LCD
#include <LiquidCrystal.h>
//TIMER
int S = 59; // count seconds
int M = 1; // count minutes
int H = 0; // count hours
//initialize the library with the numbers of the interface pins
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//BUTTON
#define BUTTON_PIN 10
byte lastButtonState = LOW;
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;
void setup() {
//LCD
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("VMPR");
lcd.setCursor(0, 1);
lcd.print("Presents...");
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Uitstelleritus");
lcd.setCursor(0, 1);
lcd.print("Deletus");
//uitstelleritus deletus
//BUTTON
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
if (millis() - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("OR ELSE...");
lcd.setCursor(6, 1);
lcd.print(":");
lcd.setCursor(9, 1);
lcd.print(":");
S--;
delay(1000);
if (S < 0) {
M--;
S = 59;
}
if (M < 0) {
H--;
M = 59;
}
if (H < 0) {
H = 0;
M = 1;
S = 59;
}
if (M > 9) {
lcd.setCursor(7, 1);
lcd.print(M);
} else {
lcd.setCursor(7, 1);
lcd.print("0");
lcd.setCursor(8, 1);
lcd.print(M);
lcd.setCursor(9, 1);
lcd.print(":");
}
if (S > 9) {
lcd.setCursor(10, 1);
lcd.print(S);
} else {
lcd.setCursor(10, 1);
lcd.print("0");
lcd.setCursor(11, 1);
lcd.print(S);
lcd.setCursor(12, 1);
lcd.print(" ");
}
if (H > 9) {
lcd.setCursor(4, 1);
lcd.print(H);
} else {
lcd.setCursor(4, 1);
lcd.print("0");
lcd.setCursor(5, 1);
lcd.print(H);
lcd.setCursor(6, 1);
lcd.print(":");
}
}
}
}