Security Lock with 4x4 Keypad and PushButton
//current password is 1234,press# key to change new password and enter new password 2 times to confirm.
#include <Keypad.h>
#include<EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
char password[4];// current password is 1234
char initial_password[4],new_password[4];// press"#" symbol to change current password
int vcc=11;
int i=0;
int button =10;// added a push button to open the lock from inside.
int relay_pin = 11;
char key_pressed=0;
const byte rows = 4;
const byte columns = 4;
char hexaKeys[rows][columns] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte row_pins[rows]={2,3,4,5};
byte column_pins[columns]={6,7,8,9};
Keypad keypad_key=Keypad( makeKeymap(hexaKeys),row_pins,column_pins,rows,columns);
void setup(){
lcd.begin();
lcd.backlight();
pinMode(relay_pin, OUTPUT);
pinMode(vcc, OUTPUT);
pinMode(button,INPUT);
lcd.print("ANOKHAUTOMATION ");
lcd.setCursor(0,1);
lcd.print("Security Lock ");
delay(3000);
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
initialpassword();
}
void loop(){
digitalWrite(relay_pin,LOW);//at initial stage ,relay will stay off
if(digitalRead(button)==HIGH)// if button pressed from inside
{digitalWrite(relay_pin,HIGH);// open the lock for 3 second
delay(3000);
digitalWrite(relay_pin,LOW);}// then back to lock position.
key_pressed = keypad_key.getKey();
if(key_pressed=='#')
change();
if (key_pressed)
{
password[i++]=key_pressed;
lcd.print("*");//Hide the password with * symbol
}
if(i==4)
{
delay(200);
for(int j=0;j<4;j++)
initial_password[j]=EEPROM.read(j);
if(!(strncmp(password, initial_password,4))){
lcd.clear();
lcd.print("Pass Accepted");
digitalWrite(relay_pin,HIGH);
delay(2000);
lcd.setCursor(0,0);
lcd.print("Pres >START< to");
lcd.setCursor(0,1);
lcd.print("change the pass");
delay(3000);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0,1);
i=0;
}
else{
digitalWrite(relay_pin,LOW);
lcd.clear();
lcd.print("Wrong Password");
lcd.setCursor(0,0);
lcd.print("Pres >#< to");
lcd.setCursor(0,1);
lcd.print("change the pass");
delay(2000);
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
i=0;
}}}
void change(){
int j=0;
lcd.clear();
lcd.print("Current Password");
lcd.setCursor(0,1);
while(j<4){
char key=keypad_key.getKey();
if(key)
{
new_password[j++]=key;
lcd.print(key);
}
key=0;}
delay(500);
if((strncmp(new_password, initial_password, 4))){
lcd.clear();
lcd.print("Wrong Password");
lcd.setCursor(0,1);
lcd.print("Try Again");
delay(1000);}
else{
j=0;
lcd.clear();
lcd.print("New Password:");
lcd.setCursor(0,1);
while(j<4){
char key=keypad_key.getKey();
if(key)
{
initial_password[j]=key;
lcd.print(key);
EEPROM.write(j,key);
j++;}}
lcd.print("Pass Changed");
delay(1000);}
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
key_pressed=0;
}
void initialpassword(){
int j;
for(j=0;j<4;j++)
EEPROM.write(j,j+49);
for(j=0;j<4;j++)
initial_password[j]=EEPROM.read(j);}
Comments
Post a Comment