#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
Servo servo1;
const uint8_t Rows = 4;
const uint8_t Cols = 4;
char Keys[Rows][Cols]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[Rows] = {A3, A2, A1, A0}; // 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);
LiquidCrystal LCD (1 , 0 , 13 , 12 , 11 , 10); // rs , e , d4 , b5 , d6, d7
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() {
LCD.begin(16,2);
servo1.attach(9);
pinMode(8, OUTPUT); //buzzer
pinMode(7, OUTPUT); //green
pinMode(6, OUTPUT); //red
}
void loop() {
char key = keypad.getKey();
if (key) {
LCD.print(key);
// 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) {
LCD.setCursor(0,1);
LCD.println(" - Access granted");
digitalWrite(7, HIGH); // Turn on LED (optional)
analogWrite(8, HIGH);
delay(3000); // Simulate successful access action (e.g., door unlock)
digitalWrite(7, LOW); // Turn off LED (optional)
analogWrite(8, LOW);
numTries = 0; // Reset attempt counter
enteredPassword = ""; // Clear entered password
} else {
LCD.setCursor(0,1);
LCD.println(" - Wrong password");
numTries++;
}
}
} else {
LCD.println(" - Maximum attempts reached. System locked.");
// Implement lockout mechanism here (e.g., delay, alarm)
}
}
}