#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>
const uint8_t rows=4;
const uint8_t cols=4;
char keys [rows][cols]{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
#define lcd_address 0x27
#define lcd_width 16
#define lcd_length 2
#define ledpin 13
#define buzzpin 12
int i;
int wrong_time;
char key;
char saved_password[6];
char input[6];
char new_password[6];
uint8_t colspin[cols] ={5,4,3,2};
uint8_t rowspin[rows]={10,9,8,7};
Keypad keypad = Keypad(makeKeymap(keys),rowspin,colspin ,rows,cols);
LiquidCrystal_I2C lcd(lcd_address,lcd_width,lcd_length);
void setup() {
// put your setup code here, to run once:
pinMode(ledpin, OUTPUT);
lcd.init();
lcd.backlight();
pinMode(buzzpin, OUTPUT);
if(EEPROM.read(10)!=1 ){
setNewPassword();}/*
if(EEPROM.read(10)!=1){
lcd.setCursor(0,0);
lcd.print("set the password");
for(i=0;i<5;i++){
while(true){
char key = keypad.getKey();
if(key!=NO_KEY){
new_password[i]=key;
EEPROM.write(i,new_password[i]);
lcd.setCursor(i,1);
lcd.print("*");
delay(300);
break;
}
}
}
new_password[5]='\0';
EEPROM.write(10,1);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("password is set");
delay(2000);
}*/
}
void loop() {
// put your main code here, to run repeatedly:
if(wrong_time >=3){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("system is lock");
lcd.setCursor(0,1);
lcd.print("try again in 10s");
delay(1000);
}
if(wrong_time >=5){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("change password");
lcd.setCursor(0,1);
lcd.print("push the * button");
delay(500);
while(true){
key=keypad.getKey();
if(key == '*'){
wrong_time=0;
setNewPassword();
break;}}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("enter password");
for(i=0;i<5;i++){
while(true){
char key=keypad.getKey() ;
if(key!=NO_KEY){
input[i]=key;
lcd.setCursor(i,1);
lcd.print("*");
delay(300);
break;
}
}
}
input[5]='\0';
for(i=0;i<5;i++){
saved_password[i]=EEPROM.read(i);
}
saved_password[5]='\0';
if(strcmp(saved_password,input)==0){
digitalWrite(ledpin, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("right password");
delay(200);
lcd.setCursor(0,1);
lcd.print("Access granted");
delay(200);
delay(5000);
digitalWrite(ledpin, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Access timeout");
delay(500);
}else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("wrong password");
wrong_time++;
digitalWrite(buzzpin, HIGH);
delay(200);
digitalWrite(buzzpin, LOW);
delay(200);
lcd.setCursor(0,1);
lcd.print("try again");
delay(200);
}
}
void setNewPassword() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("set the password");
for(i=0;i<5;i++){
while(true){
char key = keypad.getKey();
if(key!=NO_KEY){
new_password[i]=key;
EEPROM.update(i,new_password[i]);
lcd.setCursor(i,1);
lcd.print("*");
delay(300);
break;
}
}
}
new_password[5]='\0';
EEPROM.write(10, 1);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("password is set");
delay(2000);
}