#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
int buzzerPin = 48;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; // Rows 1 to 4
byte colPins[COLS] = {4, 3, 2, 10}; // Columns 1 to 4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 20, 4); // Address 0x27, 20 chars, 4 rows
char inputCode[5] = "";
char code[] = "2002"; // Password
Servo myservo; // Servo object
// Maximum number of attempts
const int maxAttempts = 3;
int attemptCount = 0; // Tracks incorrect code attempts
bool codeEntered = false; // Track if code is entered correctly
void clearInputCode() {
for (int i = 0; i < sizeof(inputCode); i++) {
inputCode[i] = '\0';
}
}
void setup() {
Serial.begin(9600);
lcd.begin(20, 4);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter code:");
pinMode(buzzerPin, OUTPUT);
myservo.attach(9);
myservo.write(0); // Set servo to initial position (0 degrees)
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (!codeEntered) {
if (attemptCount < maxAttempts) { // Only allow up to maxAttempts
if (strlen(inputCode) < 4) {
inputCode[strlen(inputCode)] = key;
lcd.setCursor(strlen(inputCode), 1);
lcd.print("*"); // Display asterisk instead of actual key
}
if (strlen(inputCode) == 4) {
lcd.setCursor(0, 1);
lcd.print(""); // Clear the line with spaces (better than multiple spaces)
lcd.setCursor(0, 1);
lcd.print(inputCode); // Display entered code with asterisks
if (strcmp(inputCode, code) == 0) {
lcd.setCursor(0, 2);
lcd.print(""); // Clear the line with spaces
lcd.setCursor(0, 2);
lcd.print("Correct code!");
delay(250); // Delay before opening the door
myservo.write(90); // Open the door
delay(5000); // Door open time
myservo.write(0); // Close the door
delay(2000);
lcd.noBacklight();
lcd.clear();
// Wait for A press to turn off LCD and clear attempt count
codeEntered = true; // Code entered correctly
} else {
attemptCount++;
lcd.setCursor(0, 2);
lcd.print(""); // Clear the line with spaces
lcd.setCursor(0, 2);
lcd.print("Wrong code!");
delay(1000); // Delay before clearing LCD
lcd.clear(); // Clear input code (already done in first call)
lcd.setCursor(0, 0);
lcd.print("Enter code:");
clearInputCode(); // Clear input code
// Sound alarm if maxAttempts is reached
if (attemptCount == maxAttempts) {
lcd.setCursor(0, 2);
lcd.print("Max attempts reached");
tone(buzzerPin, 1000); // Sound alarm
}
}
}
} else {
// Max attempts reached
while (key != 'B' && key != NO_KEY) {
key = keypad.getKey();
tone(buzzerPin, 1000); // Sound alarm
}
noTone(buzzerPin); // Stop alarm
attemptCount = 0; // Reset attempt count
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter code:");
clearInputCode(); // Clear input code
}
} else {
// Code entered correctly
if (key == 'A') {
codeEntered = false; // Reset code entered flag
lcd.clear();
lcd.backlight(); // Turn on LCD backlight
lcd.setCursor(0, 0);
lcd.print("Enter code:");
clearInputCode(); // Clear input code
}
}
}
}