#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Wire.h>
#include <Servo.h>
#define TONE 10
#define SERVO 9
#ifndef _KEY_H_
#define _KEY_H_
#include <Keypad.h>
const uint8_t ROWS = 2;
const uint8_t COLS = 1;
char keys[ROWS][COLS] = {
{ 'B'},
{ 'A' },
};
uint8_t colPins[COLS] = {6};
uint8_t rowPins[ROWS] = {2,3};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
#endif
int pos=0 ;
int sumput=0;
int Inputedpassword[6] ;
byte pass=0;
byte password[6];
int address,a=1;
Servo myservo;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup()
{
Serial.begin(9600);
Wire.begin();
lcd.init();
lcd.backlight();
EEPROM.get(6,pass);
myservo.attach(SERVO);
if(pass!=1)
{
lcd.print("Please set the ");
lcd.setCursor(0,1);
lcd.print("password");
delay(2000);
}
else
{
for(address=0;address<6;address++)
{
EEPROM.get(address,password[address]);
}
}
}
void loop()
{
char key = keypad.getKey();
if(pass!=1)
{
Setpassword();
}
else if(a)
{
Inputpassword();
}
}
void Setpassword()
{
lcd.clear();
lcd.print("password:");
address=0;
while(1)
{
char key = keypad.getKey();
if(address<6)
{
lcd.setCursor(address,1);
lcd.print(sumput);
if(key=='A')
{
sumput++;
if(sumput>=10)
{
sumput=0;
}
}
else if(key=='B')
{
password[address]=sumput;
lcd.setCursor(address,1);
lcd.print(sumput);
sumput=0;
address++;
if(address==6)
{
for(address=0;address<6;address++)
{
EEPROM.put(address,0);
EEPROM.put(address,password[address]);
}
if(pass!=1)
{
EEPROM.put(6,1);
EEPROM.get(6,pass);
lcd.clear();
lcd.print("Set successfully");
delay(1000);
lcd.clear();
}
lcd.clear();
return;
}
}
}
}
}
void Inputpassword()
{
int i=0;
a=0;
lcd.print("Enter your");
lcd.setCursor(0,1);
lcd.print("password:");
while(1)
{
char key = keypad.getKey();
lcd.setCursor(i+9,1);
lcd.print(sumput);
if(i<6)
{
if(key=='A')
{
sumput++;
if(sumput>=10)
{
sumput=0;
}
}
else if(key=='B')
{
Inputedpassword[i]=sumput;
sumput=0;
lcd.print(sumput);
i++;
}
if(i==6 && key=='B')
{
for(i=0;i<6;i++)
{
if(Inputedpassword[i]!=password[i]) a=1;
lcd.clear();
}
if(a)
{
lcd.print("Password Error");
tone(TONE,250);
if(pos>0)
{
if(pos!=90)
{
for (pos = 90; pos >= 90 ; pos --)
{
myservo.write(pos);
delay(5);
}
}
}
delay(1500);
noTone(TONE);
lcd.clear();
return;
}
else
{
lcd.print("Password Correct");
for (pos = 0; pos <= 180; pos ++)
{
myservo.write(pos);
delay(5);
}
delay(1500);
lcd.clear();
a=1;
return;
}
}
}
}
}