#include<Keypad.h>
#include<LiquidCrystal.h>
#include<EEPROM.h>
#include<Servo.h>
Servo myservo;
#define buzzer A3
int t=;
LiquidCrystal lcd(13,4,3,2,A1,A0);
int x,y;
byte dau[] = {
B01111,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B01111
};
byte cuoi[] = {
B11110,
B00001,
B00001,
B00001,
B00001,
B00001,
B00001,
B11110
};
byte muc1[] = {
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000
};
byte muc2[] = {
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000
};
byte muc3[] = {
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100
};
byte muc4[] = {
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110
};
byte muc5[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte khoa[] = {
B01110,
B10001,
B10001,
B10001,
B11111,
B11011,
B11011,
B11111
};
char password[4];
char pass[4],pass1[4];
int i=0;
char customKey=0;
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {9, 10, 11, 12};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{
myservo.attach(A2);
lcd.begin(16,2);
lcd.createChar(0,dau);lcd.createChar(6,cuoi);lcd.createChar(1,muc1);lcd.createChar(2,muc2);lcd.createChar(3,muc3);lcd.createChar(4,muc4);lcd.createChar(5,muc5);lcd.createChar(7,khoa);
pinMode(buzzer, OUTPUT);
for(int d=1;d<3;d++){
for(x=0;x<=15;x++){
lcd.setCursor(x,0);
lcd.print(char(1));
delay(t);
lcd.setCursor(x,0);
lcd.print(char(2));
delay(t);
lcd.setCursor(x,0);
lcd.print(char(3));
delay(t);
lcd.setCursor(x,0);
lcd.print(char(4));
delay(t);
lcd.setCursor(x,0);
lcd.print(char(5));
delay(t);
}
if (x>15){
lcd.clear();
}
for(y=15;y>=0;y--){
lcd.setCursor(y,1);
lcd.print(char(1));
lcd.setCursor(y,1);
lcd.print(char(2));
lcd.setCursor(y,1);
lcd.print(char(3));
lcd.setCursor(y,1);
lcd.print(char(4));
lcd.setCursor(y,1);
lcd.print(char(5));
}
delay(100);
for(y=15;y>=0;y--){
lcd.setCursor(y,1);
lcd.print(char(5));
delay(t);
lcd.setCursor(y,1);
lcd.print(char(4));delay(t);
lcd.setCursor(y,1);
lcd.print(char(3));delay(t);
lcd.setCursor(y,1);
lcd.print(char(2));delay(t);
lcd.setCursor(y,1);
lcd.print(char(1));delay(t);
lcd.setCursor(y,1);
lcd.print(" ");delay(t);
}
}
if (EEPROM.read(0)==" "){
EEPROM.write(0,1);EEPROM.write(1,1);EEPROM.write(2,1);EEPROM.write(3,1);}
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Khoi dong");
lcd.setCursor(3,1);
lcd.print("hoan tat");
delay(1000);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("San sang");
lcd.setCursor(4,1);
lcd.print("su dung");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nhap pass mo:");
lcd.setCursor(14,0);
lcd.print(char(7));
lcd.setCursor(3,1);
for(int j=0;j<4;j++)
pass[j]=EEPROM.read(j);
}
void loop()
{
myservo.write(90);
customKey = customKeypad.getKey();
if(customKey=='#')
change();
if (customKey)
{
password[i++]=customKey;
lcd.print("*");
beep();
}
if(i==4)
{
delay(200);
for(int j=0;j<4;j++)
pass[j]=EEPROM.read(j);
if(!(strncmp(password, pass,4)))
{
myservo.write(90);
beep();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Mat khau dung");
lcd.setCursor(14,0);
lcd.print(char(3));
myservo.write(180);
lcd.setCursor(7,1);
lcd.print(char(6));
delay(5000);
lcd.setCursor(0,1);
lcd.print("# Thay doi pass");
lcd.setCursor(1,1);
lcd.print(char(2));
delay(2000);
myservo.write(90);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nhap pass mo:");
lcd.setCursor(14,0);
lcd.print(char(7));
lcd.setCursor(6,1);
i=0;
myservo.write(0);
}
else
{
digitalWrite(buzzer, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Mat khau sai");
lcd.setCursor(13,0);
lcd.print("x");
lcd.setCursor(0,1);
lcd.print("# Thay doi pass");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nhap pass mo:");
lcd.setCursor(14,0);
lcd.print(char(7));
lcd.setCursor(6,1);
i=0;
digitalWrite(buzzer, LOW);
}
}
}
void change()
{
int j=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nhap pass cu:");
lcd.setCursor(14,0);
lcd.print(char(7));
lcd.setCursor(6,1);
while(j<4)
{
char key=customKeypad.getKey();
if(key)
{
pass1[j++]=key;
lcd.print("*");
beep();
}
key=0;
}
delay(500);
if((strncmp(pass1, pass, 4)))
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Khong hop le");
lcd.setCursor(0,1);
lcd.print("Hay thu lai");
delay(1000);
}
else
{
j=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nhap pass moi:");
lcd.setCursor(4,1);
while(j<4)
{
char key=customKeypad.getKey();
if(key)
{
pass[j]=key;
lcd.print(key);
EEPROM.write(j,key);
j++;
beep();
}
}
for(int z=11;z<14;z++){
lcd.setCursor(7,1);
lcd.print("Xong");
delay(500);
lcd.setCursor(z,1);
lcd.print(".");
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nhap pass mo:");
lcd.setCursor(14,0);
lcd.print(char(7));
lcd.setCursor(6,1);
customKey=0;
}
void beep()
{
digitalWrite(buzzer, HIGH);
delay(20);
digitalWrite(buzzer, LOW);
}