#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
boolean timerRunning = false;
boolean stopped = false;
boolean buttonPressed = false;
const int startButtonPin = 2; // Pin for the "Press Start" button
const int stopButtonPin = 3; // Pin for the "Stop" button
const int resetButtonPin = 4; // Pin for the "Reset" button
void setup() {
lcd.begin(16, 2);
lcd.print("Press to start");
pinMode(startButtonPin, INPUT);
pinMode(stopButtonPin, INPUT);
pinMode(resetButtonPin, INPUT);
}
void loop() {
// Check if the "Press Start" button is pressed to start or resume the timer
if (digitalRead(startButtonPin) == HIGH && !timerRunning && !stopped && !buttonPressed) {
startTime = millis() - elapsedTime;
timerRunning = true;
buttonPressed = true;
}
// Check if the "Stop" button is pressed to pause or resume the timer
if (digitalRead(stopButtonPin) == HIGH) {
if (!buttonPressed) {
buttonPressed = true;
if (timerRunning) {
elapsedTime = millis() - startTime;
timerRunning = false;
stopped = true;
lcd.setCursor(0, 1);
lcd.print(" STOP");
delay(1000); // Pause for 1 second
} else if (!timerRunning && stopped) {
startTime = millis() - elapsedTime;
timerRunning = true;
stopped = false;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the "STOP" message
}
}
} else {
buttonPressed = false;
}
// Check if the "Reset" button is pressed to clear everything and start over
if (digitalRead(resetButtonPin) == HIGH) {
startTime = 0;
elapsedTime = 0;
timerRunning = false;
stopped = false;
lcd.clear();
lcd.print("Press to start");
}
// Calculate and display the elapsed time if the timer is running
if (timerRunning) {
elapsedTime = millis() - startTime;
// Display the elapsed time on the LCD
if (elapsedTime % 1000 == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Elapsed Time:");
lcd.setCursor(0, 1);
lcd.print(elapsedTime / 1000); // Display elapsed time in seconds
lcd.print("s");
}
}
}