#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const int PASSWORD_LENGTH = 4;
const int PASSWORD[PASSWORD_LENGTH] = {1, 2, 3, 4};
const int SERVO_PIN = 11;
const int BUZZER_PIN = 12;
LiquidCrystal_I2C lcd(0x3f,16,2);
Servo servo;
// Keypad pins
const int ROW_PINS[4] = {9, 8, 7, 6};
const int COL_PINS[4] = {5, 4, 3, 2};
char KEYS[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
int passwordInput[PASSWORD_LENGTH];
int currentDigit = 0;
const char NO_KEY = '\0';
void setup()
{
lcd.init();
lcd.backlight();
lcd.print("Enter password:");
servo.attach(SERVO_PIN);
for (int row = 0; row < 4; row++) {
pinMode(ROW_PINS[row], OUTPUT);
digitalWrite(ROW_PINS[row], HIGH);
}
for (int col = 0; col < 4; col++) {
pinMode(COL_PINS[col], INPUT_PULLUP);
}
pinMode(BUZZER_PIN, OUTPUT);
}
void loop()
{
char key = getKey();
if (key != NO_KEY)
{
if (key == '#')
{
if (checkPassword()) {
lcd.clear();
lcd.print("Password OK!");
unlockDoor();
delay(2000);
lcd.clear();
lcd.print("Locked Again");
servo.write(0);
delay(2000);
lcd.clear();
lcd.print("Enter password:");
}
else {
lcd.clear();
lcd.print("Incorrect!");
buzz(3);
delay(2000);
lcd.clear();
lcd.print("Enter password:");
}
resetPassword();
} else if (key == '*') {
resetPassword();
lcd.clear();
lcd.print("Enter password:");
} else if (isdigit(key)) {
addDigit(key - '0');
}
}
}
char getKey() {
char key = NO_KEY;
for (int row = 0; row < 4; row++) {
digitalWrite(ROW_PINS[row], LOW);
for (int col = 0; col < 4; col++) {
if (digitalRead(COL_PINS[col]) == LOW) {
key = KEYS[row][col];
while (digitalRead(COL_PINS[col]) == LOW) {
delay(10);
}
}
}
digitalWrite(ROW_PINS[row], HIGH);
}
return key;
}
void addDigit(int digit) {
if (currentDigit < PASSWORD_LENGTH) {
passwordInput[currentDigit] = digit;
currentDigit++;
lcd.setCursor(currentDigit - 1, 1);
lcd.print(digit);
buzz(1); // Optional: Provide a buzz sound when a button is pressed
delay(200); // Delay for button debounce
}
}
bool checkPassword() {
for (int i = 0; i < PASSWORD_LENGTH; i++) {
if (passwordInput[i] != PASSWORD[i]) {
return false;
}
}
return true;
}
void resetPassword() {
for (int i = 0; i < PASSWORD_LENGTH; i++) {
passwordInput[i] = 0;
}
currentDigit = 0;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the password display
}
void unlockDoor() {
servo.write(90); // Open the door
}
void lockDoor() {
servo.write(0); // Close the door
}
void buzz(int count) {
for (int i = 0; i < count; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
delay(100);
}
}