// default pass is 12ABC
// enter pass and then use "#" to unlock.
// when unlocked, use # after 5 sec to lock (no pass required)
// to change pass click "*" and enter old pass.
// then click #.
// now type the new pass and click # and wait 5 sec.
// you have successfully set your new pass
#include <Keypad.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
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] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8, 9 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo myServo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int lockPosition = 90;
const int unlockPosition = 180;
String password = "12ABC";
String input = "";
bool settingNewPassword = false;
bool verifyOldPassword = false;
bool doorLocked = true;
void setup() {
Serial.begin(9600);
myServo.attach(10);
myServo.write(lockPosition);
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Enter Password:");
Serial.println("Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print(key);
lcd.setCursor(input.length(), 1);
lcd.print(key);
input += key;
if (key == '#') {
if (verifyOldPassword) {
if (input.substring(0, input.length() - 1) == password) {
Serial.println("\nOld Password Correct!");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Old Password OK");
delay(2000);
settingNewPassword = true;
verifyOldPassword = false;
input = "";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set New Password:");
Serial.println("Set New Password:");
} else {
Serial.println("\nOld Password Incorrect!");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Old Password NO");
delay(2000);
input = "";
verifyOldPassword = false;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Password:");
Serial.println("Enter Password:");
}
} else if (doorLocked) {
if (settingNewPassword) {
password = input.substring(0, input.length() - 1);
Serial.println("\nNew Password Set!");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("New Password Set!");
delay(5000);
settingNewPassword = false;
input = "";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Password:");
Serial.println("Enter Password:");
} else {
if (input.substring(0, input.length() - 1) == password) {
Serial.println("\nPassword Correct!");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Password Correct!");
unlockDoor();
delay(5000);
} else {
Serial.println("\nPassword Incorrect!");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Password Incorrect!");
delay(5000);
}
input = "";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Password:");
Serial.println("Enter Password:");
}
} else {
lockDoor();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Door Locked");
delay(5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Password:");
input = "";
Serial.println("Enter Password:");
}
}
if (key == '*') {
input = "";
verifyOldPassword = true;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Old Pass:");
Serial.println("\nEnter Old Pass:");
}
}
}
void unlockDoor() {
myServo.write(unlockPosition);
doorLocked = false;
}
void lockDoor() {
myServo.write(lockPosition);
doorLocked = true;
}