#include <EEPROM.h>
#include <Servo.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// C++ code
//
int H=0;
int R=0;
int D=0;
int A=0;
int angle=0;
int adress=2;
int number=0;
Servo servoM;
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 rowPins[ROWS] = {13, 12,11,10};
byte colPins[COLS] = {9, 8, 7,6};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup()
{
pinMode(5,INPUT);
int A=digitalRead(5);
servoM.attach(3);
angle=0;
servoM.write(angle);
lcd.init();
lcd.backlight();
while (A==HIGH){
lcd.print("new password");
// get the first number from the keypad
lcd.setCursor(0,1);
char key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
R = R * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
// clear the LCD and display message for the second number
lcd.clear();
lcd.print("confirm password");
// get the second number from the keypad
lcd.setCursor(0,1);
key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
number = number* 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
if(number !=R){
resetsetup();
}
delay(200);
break;
}while(A==LOW){
H=R;
delay(200);
break;
}
lcd.clear();
lcd.print(" door locked");
}
void loop()
{
lcd.clear();
lcd.print(" door locked");
// get the first number from the keypad
put_password();
while(D==R and D== number){
angle=90;
servoM.write(angle);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("_door Unlocked_");
lcd.setCursor(0,1);
lcd.print("_-_-_-_-_-_-_-_-");
delay(5000);
break;
}while(D!=R and D!=H){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("_password wrong_");
lcd.setCursor(0,1);
lcd.print("_-_-_-_-_-_-_-_-");
delay(500);
break;
}
D=0;
servoM.write(0);
}
void resetsetup(){
lcd.init();
lcd.backlight();
lcd.print("new password");
// get the first number from the keypad
lcd.setCursor(0,1);
char key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
R = R * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
// clear the LCD and display message for the second number
lcd.clear();
lcd.print("confirm password");
// get the second number from the keypad
lcd.setCursor(0,1);
key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
H = H * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
}
void put_password(){
lcd.setCursor(0,1);
char key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
D = D * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
}