#include <LiquidCrystal.h>
const int buzzerPin = 8;
const int buttonPin = 2;
const int ledPin = 13;
const int maxAttempts = 3; // Set the maximum number of incorrect attempts
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const char correctPIN[] = "1234"; // Set your desired PIN
char enteredPIN[5]; // Array to store entered PIN
int attempts = 0;
void setup() {
lcd.begin(16, 2);
lcd.print("Enter PIN:");
pinMode(buttonPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
readPIN();
if (checkPIN()) {
deactivateAlarm();
} else {
attempts++;
lcd.clear();
lcd.print("Incorrect! Try again");
delay(2000);
lcd.clear();
lcd.print("Enter PIN:");
if (attempts >= maxAttempts) {
activateAlarm();
lcd.clear();
lcd.print("Alarm Activated!");
while (true) {
// Loop indefinitely when alarm is activated
}
}
}
}
void readPIN() {
for (int i = 0; i < 4; i++) {
char key = getKey();
if (key != '\0') {
enteredPIN[i] = key;
lcd.setCursor(i, 1);
lcd.print('*');
}
}
enteredPIN[4] = '\0';
}
char getKey() {
char key = '\0';
while (digitalRead(buttonPin) == HIGH) {
// Wait for the button to be pressed
}
delay(50); // Debounce delay
if (digitalRead(buttonPin) == LOW) {
key = keypad.getKey();
while (digitalRead(buttonPin) == LOW) {
// Wait for the button to be released
}
}
return key;
}
bool checkPIN() {
return strcmp(enteredPIN, correctPIN) == 0;
}
void activateAlarm() {
lcd.clear();
lcd.print("Too Many Attempts!");
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000, 2000); // Activate alarm sound for 2 seconds
}
void deactivateAlarm() {
lcd.clear();
lcd.print("PIN Accepted!");
digitalWrite(ledPin, LOW);
noTone(buzzerPin); // Stop alarm sound
delay(2000);
lcd.clear();
lcd.print("Alarm Deactivated");
while (true) {
// Loop indefinitely when alarm is deactivated
}
}