#include <Keypad.h>
#define GREEN_LED D13
#define RED_LED D12
#define BUZZER D11
const byte ROWS = 4; // 4 rows
const byte COLS = 4; // 4 columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {D7, D6, D5, D4}; // Row connections
byte colPins[COLS] = {A0, A1, A2, A3}; // Column connections
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const String password = "1234"; // Set your password here
String input = ""; // Store user input
void setup() {
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
digitalWrite(GREEN_LED, LOW); // Lock is initially locked
digitalWrite(RED_LED, LOW);
Serial.begin(9600); // Initialize serial communication
Serial.println("Password-Protected Lock System");
Serial.println("Enter the password using the keypad:");
}
void loop() {
char key = keypad.getKey();
if (key) {
beep(); // Play a beep on key press
Serial.print("Key Pressed: ");
Serial.println(key);
if (key == '#') { // Submit password
Serial.print("Entered Password: ");
Serial.println(input);
if (input == password) {
unlock();
Serial.println("Correct Password: Door Unlocked!");
} else {
errorFeedback();
Serial.println("Incorrect Password: Access Denied!");
}
input = ""; // Reset input after each attempt
} else if (key == '*') { // Clear input
input = "";
Serial.println("Input cleared.");
} else {
input += key; // Append the key to the input string
Serial.print("Current Input: ");
Serial.println(input);
}
}
}
void beep() {
digitalWrite(BUZZER, HIGH);
delay(100);
digitalWrite(BUZZER, LOW);
}
void unlock() {
digitalWrite(GREEN_LED, HIGH); // Green LED ON
delay(2000); // Keep it on for 2 seconds
digitalWrite(GREEN_LED, LOW); // Lock again
}
void errorFeedback() {
for (int i = 0; i < 3; i++) { // Flash the red LED 3 times
digitalWrite(RED_LED, HIGH);
beep(); // Error beep
delay(300);
digitalWrite(RED_LED, LOW);
delay(300);
}
}