#include <Keypad.h>
// Define the password
const String password = "1234";
String inputPassword = "";
// Define LED and buzzer pins
const int ledPin = 12;
const int buzzerPin = 4; // New pin for the buzzer
// Define keypad size and connections
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] = {13, 14, 27, 26}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {25, 33, 32, 15}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
bool ledState = false;
unsigned long startMillis = 0;
const long delayTime = 10000; // 10 seconds delay
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
digitalWrite(ledPin, LOW); // Start with the LED off
digitalWrite(buzzerPin, LOW); // Start with the buzzer off
}
void loop() {
char key = keypad.getKey(); // Get key press
if (key) { // If a key is pressed
Serial.println(key); // Print the pressed key
if (key == '#') { // # key to submit the password
if (inputPassword == password) {
Serial.println("Password Correct!");
digitalWrite(ledPin, HIGH); // Turn on the LED
ledState = true;
startMillis = millis(); // Save the time LED was turned on
} else {
Serial.println("Incorrect Password!");
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer for incorrect password
delay(1000); // Buzzer sound duration (1 second)
}
inputPassword = ""; // Clear the input for the next attempt
} else if (key == '*') { // * key to clear the input
inputPassword = "";
Serial.println("Input Cleared");
} else {
inputPassword += key; // Add the key to the password input
}
}
// Check if LED should be turned off after 10 seconds
if (ledState && (millis() - startMillis >= delayTime)) {
digitalWrite(ledPin, LOW); // Turn off the LED
ledState = false;
Serial.println("LED turned off after 10 seconds");
}
}