#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo myservo;
int pos = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
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 rowPin[ROWS] = {2, 3, 4, 5};
byte colsPin[COLS] = {6, 7, 8, 9};
Keypad customKeypad = Keypad(makeKeymap(Keys), rowPin, colsPin, ROWS, COLS);
#define Password_Length 7
char Data[Password_Length];
char Master[Password_Length] = "452333";
byte data_count = 0;
char customKey;
bool door = true;
int wrongPasswordCount = 0;
const int buzzerPin = 11;
bool changePasswordMode = false;
bool verifyOldPassword = true;
char NewPassword[Password_Length];
byte newPasswordCount = 0;
void setup() {
lcd.init();
lcd.backlight();
myservo.attach(10);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
if (changePasswordMode) {
changePassword();
} else if (door == 0) {
customKey = customKeypad.getKey();
if (customKey == '#') {
lcd.clear();
ServoClose();
lcd.print(" Door is close");
delay(3000);
door = 1;
}
} else {
Open();
}
}
void Open() {
lcd.setCursor(0, 0);
lcd.print(" Enter Password");
customKey = customKeypad.getKey();
if (customKey) {
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
}
if (data_count == Password_Length - 1) {
if (!strcmp(Data, Master)) {
lcd.clear();
ServoOpen();
lcd.print(" Door is Open");
door = 0;
wrongPasswordCount = 0;
} else {
wrongPasswordCount++;
lcd.clear();
lcd.print(" Wrong Password");
delay(1000);
if (wrongPasswordCount >= 3) {
soundBuzzer();
wrongPasswordCount = 0;
}
}
clearData();
}
if (customKey == '*') {
lcd.clear();
lcd.print("Old Password");
delay(1000);
clearData();
changePasswordMode = true;
verifyOldPassword = true;
}
}
void changePassword() {
if (verifyOldPassword) {
lcd.setCursor(0, 0);
lcd.print("Old Password");
customKey = customKeypad.getKey();
if (customKey) {
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
}
if (data_count == Password_Length - 1) {
if (!strcmp(Data, Master)) {
lcd.clear();
lcd.print("New Password");
delay(1000);
clearData();
verifyOldPassword = false;
} else {
lcd.clear();
lcd.print("Wrong Password");
delay(2000);
clearData();
changePasswordMode = false;
}
}
} else {
lcd.setCursor(0, 0);
lcd.print(" New Password");
customKey = customKeypad.getKey();
if (customKey) {
NewPassword[newPasswordCount] = customKey;
lcd.setCursor(newPasswordCount, 1);
lcd.print(NewPassword[newPasswordCount]);
newPasswordCount++;
}
if (newPasswordCount == Password_Length - 1) {
strcpy(Master, NewPassword);
lcd.clear();
lcd.print("Password Changed");
delay(2000);
clearData();
newPasswordCount = 0;
changePasswordMode = false;
}
}
}
void clearData() {
while (data_count != 0) {
Data[data_count--] = 0;
}
}
void ServoOpen() {
for (pos = 180; pos >= 0; pos -= 5) {
myservo.write(pos);
delay(15);
}
}
void ServoClose() {
for (pos = 0; pos <= 180; pos += 5) {
myservo.write(pos);
delay(15);
}
}
void soundBuzzer() {
digitalWrite(buzzerPin, HIGH);
delay(5000);
digitalWrite(buzzerPin, LOW);
}