#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the LCD
const int startStopButtonPin = 2; // Pin for start/stop button
const int resetButtonPin = 3; // Pin for reset button
unsigned long startTime = 0; // Variable to store the start time
unsigned long elapsedTime = 0; // Variable to store the elapsed time
bool isRunning = false; // Flag to indicate if the stopwatch is running
void setup() {
lcd.begin(16, 2); // Set up the LCD columns and rows
lcd.print("Press Start"); // Display initial message
pinMode(startStopButtonPin, INPUT_PULLUP); // Set start/stop button pin as input with pull-up resistor
pinMode(resetButtonPin, INPUT_PULLUP); // Set reset button pin as input with pull-up resistor
}
void loop() {
if (digitalRead(startStopButtonPin) == LOW) {
if (!isRunning) {
startTime = millis(); // Start the stopwatch
isRunning = true;
lcd.clear();
} else {
isRunning = false; // Stop the stopwatch
}
delay(200); // Debounce delay
}
if (digitalRead(resetButtonPin) == LOW) {
if (!isRunning) {
elapsedTime = 0; // Reset the stopwatch
lcd.clear();
lcd.print("Press Start");
}
delay(200); // Debounce delay
}
if (isRunning) {
// Calculate elapsed time
unsigned long currentTime = millis();
elapsedTime = currentTime - startTime;
// Display elapsed time on the LCD
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.setCursor(6, 0);
lcd.print(elapsedTime / 1000); // Convert milliseconds to seconds
lcd.print(" s");
}
}