#include <Keypad.h>
#include <LiquidCrystal.h>
#define BUZZER_PIN 7
#define PASSWORD "1234" // Set your desired password here
// Define LCD pins and initialize the LCD
LiquidCrystal lcd(2, 3, 4, 6);
// Define keypad configuration
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
bool running = false;
bool alertActive = false;
String inputPassword = "";
// Function to start the stopwatch
void startStopwatch() {
startTime = millis();
running = true;
alertActive = false;
inputPassword = ""; // Reset password entry
lcd.clear();
lcd.print("Stopwatch Started");
}
// Function to stop the stopwatch
void stopStopwatch() {
running = false;
lcd.clear();
lcd.print("Stopwatch Stopped");
delay(1000);
}
// Function to reset the stopwatch
void resetStopwatch() {
startTime = millis();
lcd.clear();
lcd.print("Reset Stopwatch");
delay(1000);
lcd.clear();
}
void setup() {
lcd.begin(16, 2); // Initialize 16x2 LCD
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
lcd.print("Press A to Start");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == 'A' && !running) {
startStopwatch();
} else if (key == 'B' && running) {
stopStopwatch();
} else if (key == 'C' && !running) {
resetStopwatch();
} else if (key == '#') {
inputPassword = ""; // Clear input if '#' is pressed
} else if (isdigit(key)) {
inputPassword += key;
// Check if password is correct
if (inputPassword == PASSWORD) {
lcd.clear();
lcd.print("Password Correct");
delay(1000);
inputPassword = "";
alertActive = false;
digitalWrite(BUZZER_PIN, LOW); // Stop buzzer if correct password is entered
} else if (inputPassword.length() >= 4) {
lcd.clear();
lcd.print("Wrong Password");
delay(1000);
inputPassword = ""; // Reset on incorrect password
}
}
}
if (running) {
elapsedTime = (millis() - startTime) / 1000;
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(elapsedTime);
lcd.print(" s");
// Activate buzzer if stopwatch runs for more than 30 seconds
if (elapsedTime > 30 && !alertActive) {
digitalWrite(BUZZER_PIN, HIGH);
alertActive = true;
}
}
}