#include <LiquidCrystal.h>
#include <Keypad.h>
// Set up the LCD
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
// Set up the keypad
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, 12, 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Set up the pins
const int lightPin1 = 4;
const int lightPin2 = 3;
const int lightPin3 = 2;
const int lightPin4 = 1;
const int buzzerPin = 5;
// Define the correct PIN
String correctPin = "1234";
// Variables to store the entered PIN and attempt count
String enteredPin = "";
int attemptCount = 0;
void setup() {
// Initialize the LCD
lcd.begin(16, 2);
lcd.print("Enter PIN:-");
// Initialize the pins
pinMode(lightPin1, OUTPUT);
pinMode(lightPin2, OUTPUT);
pinMode(lightPin3, OUTPUT);
pinMode(lightPin4, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Check if the entered PIN is correct
if (enteredPin == correctPin) {
// Correct PIN entered
digitalWrite(lightPin4, HIGH);
tone(buzzerPin, 1000, 500); // Happy beep
lcd.clear();
lcd.print("Access Granted");
delay(2000); // Wait for 2 seconds
resetSystem();
} else {
// Incorrect PIN entered
attemptCount++;
if (attemptCount >= 3) {
// Three incorrect attempts
lockForever();
} else if (attemptCount == 2) {
// Two incorrect attempts
digitalWrite(lightPin1, HIGH);
digitalWrite(lightPin2, HIGH);
tone(buzzerPin, 1000, 500); // Beep
lcd.clear();
lcd.print("Wrong PIN");
delay(2000); // Wait for 2 seconds
lcd.clear();
lcd.print("Enter PIN:");
} else if (attemptCount == 1) {
// One incorrect attempt
digitalWrite(lightPin1, HIGH);
tone(buzzerPin, 1000, 500); // Beep
lcd.clear();
lcd.print("Wrong PIN");
delay(2000); // Wait for 2 seconds
lcd.clear();
lcd.print("Enter PIN:");
}
}
// Reset the entered PIN
enteredPin = "";
} else if (key == '*') {
// Reset the entered PIN
enteredPin = "";
lcd.clear();
lcd.print("Enter PIN:");
} else {
// Append the key to the entered PIN
enteredPin += key;
lcd.setCursor(0, 1);
lcd.print(enteredPin);
}
}
}
void lockForever() {
lcd.clear();
lcd.print("Locked Forever");
for (int i = 0; i < 20; i++) { // Blink for 10 seconds
digitalWrite(lightPin1, HIGH);
digitalWrite(lightPin2, HIGH);
digitalWrite(lightPin3, HIGH);
delay(250);
digitalWrite(lightPin1, LOW);
digitalWrite(lightPin2, LOW);
digitalWrite(lightPin3, LOW);
delay(250);
}
digitalWrite(lightPin1, HIGH);
digitalWrite(lightPin2, HIGH);
digitalWrite(lightPin3, HIGH);
tone(buzzerPin, 1000, 2000); // Long beep
while (true); // Infinite loop to lock the system
}
void resetSystem() {
// Reset the system to initial state
enteredPin = "";
attemptCount = 0;
digitalWrite(lightPin1, LOW);
digitalWrite(lightPin2, LOW);
digitalWrite(lightPin3, LOW);
digitalWrite(lightPin4, LOW);
noTone(buzzerPin);
lcd.clear();
lcd.print("Enter PIN:");
}