CONNECTION DIAGRAM
CODE
#include <EEPROM.h>
#include "Keypad.h"
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
#define buzzer 13 // choose the pin for the buzzer
#define door_O_pin 10
#define door_C_pin 9
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// this may be different on different keypads - please check!
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the keypad
// attach keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char password[4];
char initial_password[4],new_password[4];
char key_pressed=0;
int cnt=0, Check=0;
int set_time=8; //Set the timer here for how long the door should stay open
void setup(){ // put your setup code here, to run once
pinMode(door_O_pin,OUTPUT);
pinMode(door_C_pin,OUTPUT);
pinMode(buzzer,OUTPUT); // declare buzzer as output
lcd.begin();// initialize the lcd
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(" Password Lock ");
lcd.setCursor(0,1);
lcd.print("Security System");
delay(2000); // Waiting for a while
lcd.clear();
lcd.print(" Press # for ");
lcd.setCursor(0,1);
lcd.print("Change Password");
delay(2000); // Waiting for a while
lcd.clear();
lcd.print("Enter Password: "); //What's written on the lcd you can change
if(EEPROM.read(100)==0){}
else{
for(int i=0;i<4;i++){EEPROM.write(i, i+49);}
EEPROM.write(100, 0);
}
for(int i=0;i<4;i++){ //read code from EEPROM
initial_password[i]=EEPROM.read(i);
}
digitalWrite(door_O_pin, LOW); // Door Open
digitalWrite(door_C_pin, HIGH);// Door Close
delay(2000); // Waiting for a while
digitalWrite(door_O_pin, LOW); // Door Open
digitalWrite(door_C_pin, LOW); // Door Close
}
void loop(){
key_pressed = keypad.getKey(); // get a key (if pressed)
if(key_pressed=='#'){change();}
if(cnt>=4){delay(200);
for(int j=0;j<4;j++){initial_password[j]=EEPROM.read(j);}
if(!(strncmp(password, initial_password,4))){ Check=0;
lcd.clear();
lcd.print("Please Wait... ");
lcd.setCursor(0,1);
lcd.print(" Door is Opening");
digitalWrite(door_O_pin, HIGH);// Door Open
digitalWrite(door_C_pin, LOW); // Door Close
delay(600); // Waiting for a while
digitalWrite(door_O_pin, LOW); // Door Open
digitalWrite(door_C_pin, LOW); // Door Close
lcd.clear();
lcd.print(" Welcome ");
lcd.setCursor(0,1);
lcd.print(" Door is Open ");
for(int i=0;i<set_time;i++){delay(500);}
lcd.clear();
lcd.print("Please Wait... ");
lcd.setCursor(0,1);
lcd.print(" Door is Closing");
digitalWrite(door_O_pin, LOW); // Door Open
digitalWrite(door_C_pin, HIGH);// Door Close
delay(600); // Waiting for a while
digitalWrite(door_O_pin, LOW); // Door Open
digitalWrite(door_C_pin, LOW); // Door Close
delay(100);
lcd.clear();
lcd.print("Enter Password:");
cnt=0;
}else{ Check = Check+1;
lcd.clear();
lcd.print("Wrong Password");
lcd.setCursor(0,1);
lcd.print("Pres # to Change");
if(Check>=3){ Check=0;
lcd.clear();
lcd.print("Please Wait... ");
for(int i=10; i>0; i--){
lcd.setCursor(0,1);
lcd.print(" Time: ");
lcd.print((i/10)%10);
lcd.print(i%10);
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
delay(500);
}
}else{
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
}
lcd.clear();
lcd.print("Enter Password:");
cnt=0;
}
}
if(key_pressed=='*'){
if(cnt>0)password[cnt--];
lcd.setCursor(5+cnt,1);
if(cnt>0)lcd.print("* ");
else lcd.print(" ");
digitalWrite(buzzer, HIGH);
delay(100);
}else{
if(key_pressed){
password[cnt++]=key_pressed;
lcd.setCursor(5+cnt,1);
lcd.print("* ");
digitalWrite(buzzer, HIGH);
delay(100);
}
}
delay(100);
digitalWrite(buzzer, LOW);
}
void change(){
int j=0;
lcd.clear();
lcd.print("Current Password");
lcd.setCursor(5,1);
digitalWrite(buzzer, HIGH); delay(100);
digitalWrite(buzzer, LOW);
while(j<4){
char key=keypad.getKey();
if(key){
digitalWrite(buzzer, HIGH); delay(200);
digitalWrite(buzzer, LOW);
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(5,1);
while(j<4){
char key=keypad.getKey();
if(key){
digitalWrite(buzzer, HIGH); delay(200);
digitalWrite(buzzer, LOW);
initial_password[j]=key;
lcd.print(key);
EEPROM.write(j,key);
j++;
}
}
lcd.clear();
lcd.print("Pass Changed");
delay(1000);
}
lcd.clear();
lcd.print("Enter Password:");
key_pressed=0;
}