#include <Keypad.h>
const byte ROWS = 4; // Number of rows on the keypad
const byte COLS = 4; // Number of columns on the keypad
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}; // Pins connected to keypad rows
byte colPins[COLS] = {5, 4, 3, 2}; // Pins connected to keypad columns
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const char* correctPassword = "1234"; // Set your desired password here (change to a stronger password)
int numTries = 0; // Track the number of attempted entries
const int maxTries = 3; // Maximum allowed attempts before lockout
void setup() {
Serial.begin(9600); // Optional serial communication for debugging
pinMode(11,OUTPUT);
pinMode(10, OUTPUT); // Pin connected to an LED (optional for visual feedback)
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print(key); // Optional: Print entered key for debugging
// Password entry logic with lockout and visual feedback (LED)
if (numTries < maxTries) {
static String enteredPassword = ""; // Clear entered password on each new attempt
enteredPassword += key;
if (enteredPassword.length() == strlen(correctPassword)) {
if (enteredPassword == correctPassword) {
Serial.println(" - Access granted");
digitalWrite(10, HIGH); // Turn on LED (optional)
analogWrite(11, HIGH);
delay(3000); // Simulate successful access action (e.g., door unlock)
digitalWrite(10, LOW); // Turn off LED (optional)
analogWrite(11, LOW);
numTries = 0; // Reset attempt counter
enteredPassword = ""; // Clear entered password
} else {
Serial.println(" - Wrong password");
numTries++;
}
}
} else {
Serial.println(" - Maximum attempts reached. System locked.");
// Implement lockout mechanism here (e.g., delay, alarm)
}
}
}