#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int button1 = A0;
const int button2 = A1;
const int resetButton = A2;
const int stopButton = A3;
const int outputPin = 7;
unsigned long countDownTime = 0; // in seconds
unsigned long remainingTime = 0; // in seconds
bool isCountingDown = false;
bool isResetting = false;
void setup() {
lcd.begin(16, 2);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(resetButton, INPUT_PULLUP);
pinMode(stopButton, INPUT_PULLUP);
pinMode(outputPin, OUTPUT);
lcd.print("Press button 1");
lcd.setCursor(0, 1);
lcd.print("add 10s of time");
}
void loop() {
// check if button 1 is pressed
if (digitalRead(button1) == LOW && !isCountingDown && !isResetting) {
countDownTime += 10;
lcd.clear();
lcd.print("Added 10s.");
lcd.setCursor(0, 1);
lcd.print("Total: ");
lcd.print(countDownTime);
lcd.print("s");
delay(500);
}
// check if button 2 is pressed
if (digitalRead(button2) == LOW && !isCountingDown && !isResetting && countDownTime > 0) {
remainingTime = countDownTime;
isCountingDown = true;
digitalWrite(outputPin, HIGH);
lcd.clear();
lcd.print("Countdown started");
lcd.setCursor(0, 1);
lcd.print("Remaining: ");
lcd.print(remainingTime);
lcd.print("s");
delay(500);
}
// check if reset button is pressed
if (digitalRead(resetButton) == LOW && !isCountingDown && !isResetting) {
countDownTime = 0;
lcd.clear();
lcd.print("Time reset to 0s");
delay(500);
}
// check if stop button is pressed
if (digitalRead(stopButton) == LOW && isCountingDown) {
isCountingDown = false;
digitalWrite(outputPin, LOW);
lcd.clear();
lcd.print("Countdown stopped");
lcd.setCursor(0, 1);
lcd.print("Remaining: ");
lcd.print(remainingTime);
lcd.print("s");
delay(500);
}
// update remaining time if counting down
if (isCountingDown) {
lcd.clear();
lcd.print("Counting down...");
lcd.setCursor(0, 1);
lcd.print("Remaining: ");
lcd.print(remainingTime);
lcd.print("s");
delay(1000);
if (remainingTime > 0) {
remainingTime--;
} else {
isCountingDown = false;
digitalWrite(outputPin, LOW);
lcd.clear();
lcd.print("Countdown finished");
lcd.setCursor(0, 1);
lcd.print("Set the time");
delay(2000);
remainingTime = 0;
countDownTime = 0;
}
}
}