#include <Keypad.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <Servo.h>
Servo myservo;
#define LEDindicator A3 //Door Open/close LED indicator
#define BypassSwitch A2 //Push Button for emergency bypass
int pos = 0; // variable to store the servo position
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
char keypressed;
char code[]= {'1','2','3','4'}; //The default password
char code_buff1[sizeof(code)]; //Where the new password is stored
char code_buff2[sizeof(code)]; //Where the new password is stored again
short a=0,i=0,s=0,j=0; //Variables
byte rowPins[numRows] = {8,9,10,11}; //Rows 0 to 3
byte colPins[numCols]= {2,3,4,5}; //Columns 0 to 3
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
int count_wrong = 0;
int speaker = 13;
void setup()
{
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("ACCESS PROTECTED");
lcd.setCursor(0,1);
lcd.print("ENTER *(PW)0 ");
myservo.attach(7); // attaches the servo on pin 9 to the servo object
pinMode(LEDindicator,OUTPUT);
pinMode(speaker,OUTPUT);
pinMode(BypassSwitch,INPUT);
for(i=0 ; i<sizeof(code);i++){ // uncomment
EEPROM.write(i, code[i]); // uncomment
// Serial.println(code[i]);
// EEPROM.commit();
}
// EEPROM.commit();
for(i=0 ; i<sizeof(code);i++){ // uncomment
EEPROM.get(i, code[i]); // uncomment
Serial.println(code[i]);
}
}
void loop()
{
if (count_wrong>= 3){
digitalWrite(speaker, HIGH);
}
else digitalWrite(speaker, LOW);
keypressed = myKeypad.getKey(); //4x4 key to be pressed
if(keypressed == '*'){ // * to open the lock
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ENTER (PW)0 ");
GetCode(); //Getting code
if(a==sizeof(code)) {
OpenDoor();
count_wrong = 0;
} //The size of the code array
else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WRONG PASSWORD ");
lcd.setCursor(0,1);
lcd.print("TRY AGAIN ");
count_wrong += 1;
Serial.println(count_wrong);
}
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ACCESS PROTECTED");
lcd.setCursor(0,1);
lcd.print("ENTER *(PW)0 ");
}
if(keypressed == '#'){ //To change the code it calls the Password Change function
PWChange();
// lcd.clear();
// lcd.print("ENTER PASSWORD ");
}
if(digitalRead(BypassSwitch)==HIGH){ //Opening door
digitalWrite(LEDindicator,HIGH);
lcd.setCursor(0,0);
lcd.print("BYPASS ACTIVATED");
lcd.setCursor(0,1);
lcd.print("DOOR OPENING....");
for (pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
delay(3000);
for (pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
// delay(3000); //Opens for 3s you can change
digitalWrite(LEDindicator,LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ACCESS PROTECTED");
lcd.setCursor(0,1);
lcd.print("ENTER *(PW)0 ");
}
}
void GetCode(){ //Getting code sequence
i=0; //All variables set to 0
a=0;
j=0;
while(keypressed != '0'){ //The user press 0 to confirm the code otherwise he can keep typing
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != '0' ){ //If the char typed isn't 0 and neither "nothing"
lcd.setCursor(j,1); //This to write "*" on the LCD whenever a key is pressed it's position is controlled by j
lcd.print("*");
j++;
if(keypressed == code[i]&& i<sizeof(code)){ //if the char typed is correct a and i increments to verify the next caracter
a++; //Now I think maybe I should have use only a or i ... too lazy to test it -_-'
i++;
}
else
a--; //if the character typed is wrong a decrements and cannot equal the size of code []
}
}
keypressed = NO_KEY;
}
void PWChange(){ //Change code sequence
lcd.clear();
lcd.print("PASSWORD CHANGE ");
delay(1000);
lcd.clear();
lcd.print("ENTER (old PW)0 ");
GetCode(); //verify the old code first so you can change it
if(a==sizeof(code)){ //again verifying the a value
lcd.clear();
lcd.print("PASSWORD CHANGE ");
PWChangeCode(); //Get the new code
PWChangeconfirm(); //Get the new code again to confirm it
s=0;
for(i=0 ; i<sizeof(code) ; i++){ //Compare codes in array 1 and array 2 from two previous functions
if(code_buff1[i]==code_buff2[i])
s++; //again this how we verifiy, increment s whenever codes are matching
}
if(s==sizeof(code)){ //Correct is always the size of the array
for(i=0 ; i<sizeof(code) ; i++){
code[i]=code_buff2[i]; //the code array now receives the new code
EEPROM.put(i, code[i]); //And stores it in the EEPROM
//Serial.print(EEPROM.get(i, code[i])); //password
}
lcd.clear();
lcd.print("PASSWORD CHANGED");
delay(2000);
}
else{ //In case the new codes aren't matching
lcd.clear();
lcd.print("NEW PASSWORD ARE");
lcd.setCursor(0,1);
lcd.print("NOT MATCHING !! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("WRONG PASSWORD ");
lcd.setCursor(0,1);
lcd.print("TRY AGAIN ");
}
}
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ACCESS PROTECTED");
lcd.setCursor(0,1);
lcd.print("ENTER *(PW)0 ");
}
void PWChangeCode(){
i=0;
j=0;
lcd.clear();
lcd.print("CHANGE TO NEW PW");
lcd.setCursor(0,1);
lcd.print("ENTER (new PW)0 ");
delay(2000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("ENTER (new PW)0 ");
while(keypressed != '0'){ //0 to confirm and quits the loop
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != '0' ){
lcd.setCursor(j,0);
lcd.print("*"); //On the new code you can show * as I did or change it to keypressed to show the keys
code_buff1[i]=keypressed; //Store caracters in the array
i++;
j++;
}
}
keypressed = NO_KEY;
}
void PWChangeconfirm(){ //This is exactly like the ChangeCode function but this time the code is stored in another array
i=0;
j=0;
lcd.clear();
lcd.print("CONFIRM PASSWORD");
lcd.setCursor(0,1);
lcd.print("ENTER (new PW)0 ");
delay(3000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("ENTER (new PW)0 ");
while(keypressed != '0'){
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != '0' ){
lcd.setCursor(j,0);
lcd.print("*");
code_buff2[i]=keypressed;
i++;
j++;
}
}
keypressed = NO_KEY;
}
void OpenDoor(){ //Door opening function open for 3s
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ACCESS GRANTED ");
lcd.setCursor(0,1);
lcd.print("DOOR OPENING....");
digitalWrite(LEDindicator,HIGH);
for (pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
delay(3000);
for (pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
digitalWrite(LEDindicator,LOW);
}