#include <LiquidCrystal_I2C.h>
// Initialize the LCD with I2C address (usually 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int startButton = 25; // GPIO for start button
const int stopButton = 26; // GPIO for stop button
bool isRunning = false;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
void setup() {
// Initialize LCD
lcd.begin();
lcd.backlight();
lcd.print("Stopwatch Ready");
// Configure buttons
pinMode(startButton, INPUT_PULLDOWN);
pinMode(stopButton, INPUT_PULLDOWN);
// Clear LCD after 2 seconds
delay(2000);
lcd.clear();
}
void loop() {
if (digitalRead(startButton) == HIGH) {
if (!isRunning) {
isRunning = true;
startTime = millis();
lcd.clear();
lcd.print("Running...");
delay(300); // Debounce delay
}
}
if (digitalRead(stopButton) == HIGH) {
if (isRunning) {
isRunning = false;
elapsedTime = millis() - startTime;
lcd.clear();
lcd.print("Time: ");
lcd.setCursor(0, 1);
lcd.print(elapsedTime / 1000); // Convert to seconds
lcd.print(" sec");
delay(300); // Debounce delay
}
}
if (isRunning) {
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print((millis() - startTime) / 1000); // Live time in seconds
lcd.print(" sec");
}
}