Password lock with EEPROM

Hello,

A year ago i built a password lock with an arduino mega 2560, a 16x2 LCD and a 4x4 keypad.
A while ago i decided to upgrade it with a new feature: possibility to change the password with the keyboard (so without uploading a new code). I found out the best way to do this is with EEPROm.
After a lot of research i still can’t find out how to do it.

This is my code:

#include <EEPROM.h>
#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>

//EEPROM
int address = 0;
byte value;

Servo myservo;

//constants for LEDs
int greenLED = 22;
int redLED = 23;

LiquidCrystal lcd(13, 12, 11, 10, 9, 8, 7);
Password password = Password( "4155" );

int currentPosition = 0; //servo

//keypad part
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {
    '1','2','3','A',  }
  ,
  {
    '4','5','6','B',  }
  ,
  {
    '7','8','9','C',  }
  ,
  {
    '*','0','#','D',  }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {
  47, 46, 45, 44}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  51, 50, 49, 48}; //connect to the column pinouts of the keypad

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  lcd.begin(16, 2);
  Serial.begin(9600);
  keypad.setDebounceTime(50);
  displayCodeEntryScreen();
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  //setup and turn off both LEDs
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, LOW);
}

void displayCodeEntryScreen(){
  password.reset();
  lcd.clear();
  lcd.print("Enter Password:");
  lcd.setCursor(0,1);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  //setup and turn off both LEDs
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, LOW);
}

void loop(){
  keypad.getKey();
  value = EEPROM.read(address);

}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:
    lcd.print(eKey);
    switch (eKey){
    case '#': 
      checkPassword();
      break;
    case 'C':
      changepassword();
      break;
    case 'D':
      lockdoor();
      break;
    case '*': 
         displayCodeEntryScreen(); break;
    default:
      password.append(eKey);
    }
  }
  }

void checkPassword(){ //checks if the password is correct
  if (password.evaluate()){
    digitalWrite(greenLED, HIGH);
    lcd.clear();
    delay(30);
    lcd.setCursor(1, 0);
    lcd.print("Acces Granted");
    lcd.setCursor(4, 1);
    lcd.print("Welcome");
    unlockdoor();
    delay(2500);
    displayCodeEntryScreen();
  }
  else{
    loop(); {
      redlight();
    }
    lcd.clear();
    delay(10);
    lcd.setCursor(2, 0);
    lcd.print("Acces Denied");
    delay(2500);
    lcd.clear();
    displayCodeEntryScreen();
  }
}

void unlockdoor(){ //controls servo that locks the door
  myservo.attach(2);
  myservo.write(90);
  digitalWrite(greenLED, HIGH);
  delay(5000);
  myservo.detach();
}

void redlight(){ //flashes the red LED when password is wrong
digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, LOW);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, LOW);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, LOW);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, LOW);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, LOW);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, LOW);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, LOW);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);
}

void changepassword() {
  if (password.evaluate()){
    lcd.print("Enter New");
    lcd.setCursor(0, 1);
    lcd.print("Password:");
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("Password");
    lcd.setCursor(4, 1);
    lcd.print("Changed");
    delay(3000);
  }
  else{
    loop(); {
      redlight();
    }
    lcd.clear();
    delay(10);
    lcd.print("Wrong Password");
    delay(2500);
    lcd.clear();
    displayCodeEntryScreen();
    
  }
  
  
}

void lockdoor(){ //locks the door again
  myservo.attach(2);
  myservo.write(180);
  delay(2000);
  myservo.detach();
}

What i need to know is how to get to write and read that password code in the EEPROM.
But to keep the password function from the password.h library i must keep the following part of the code for sure:

Password password = Password( "4155" );

Can i change the part between the brackets to a reference to the EEPROM value?
How would you insert this feature in the code?

4 characters in EEPROM will be 4 separate reads. Put the characters you read into a char array and pass them into that Password constructor.

char myPass[4];

myPass[0] = EEPROM.read(0);  // assuming the password starts at EEPROM address 0
myPass[1] = EEPROM.read(1);
myPass[2] = EEPROM.read(2);
myPass[3] = EEPROM.read(3);

Password password = Password(myPass);

That may not be the exact code you need, but it should at least get you pointed in the right direction.

I have the same problem… my code is…

#include<LiquidCrystal.h>
#include <Servo.h>
#include <EEPROM.h>

Servo ser;
LiquidCrystal lcd(7,6,5,4,3,2);
int row={13, 12,A5,A4};// Defining row pins of keypad connected to Arduino pins
int col={A0,A1,A2,A3};//Defining column pins of keypad connected to Arduino
int i,j,lcd_count,count=1,key_id=0,flag,entry=0;// See About the Program
int col_scan;// Variable to identify a key press
char temp_press; // Variable to hold value of key press
char check[6],pass[6]; // See About the Program
#define red 8
int bajaw = 0;
int buzzer = 11;

void setup()
{
Serial.begin(9600);

ser.attach(9);
ser.write(90);
pinMode(buzzer, OUTPUT);
lcd.begin(20,4);
pinMode(red, OUTPUT);

for(i=0;i<=3;i++)
{
pinMode(row*,OUTPUT);*
pinMode(col*,INPUT);*
digitalWrite(col*,HIGH);*
}
* lcd.clear();*
* lcd.setCursor(0,0);*
* lcd.print(“Smart Home”);*
* lcd.setCursor(2,1);*
* lcd.print(“Lock System”);*
* lcd.setCursor(4,6);*
* lcd.print(“PU”);*
* delay(3000);*
* lcd.clear();*
* if(EEPROM.read(1) == NULL){*
* lcd.print(“SET 5 Digit PASS”);*
* } else {*
* lcd.print(“Give Password…”);*
* }*
}
/* Main Program Begins */
void loop()
{
* if(EEPROM.read(1) == NULL){*
* while(entry<=4)// Password Setting Loop begins*
* {*
* SetPassword();*
* }*
* // Password Setting Loop Ends*
* }*

* key_id=0;
_ keyscan(); // Scan for a Key Press*
/* Actions on Key Press begins /
if(key_id==1) // Condition to Check Key is Pressed
{
check[count]=temp_press;
count++;
/ Condition to Unlock Begins*/

if(temp_press==‘A’)
{
* checkPassword();
if(flag==0)
{
lcd.setCursor(0,0);
digitalWrite(red, LOW);
delay(100);
lcd.print(“UNLOCKED”);
}else{
lcd.setCursor(0,0);
lcd.print(“WRONG PASSWORD”);
bajaw++;
digitalWrite(buzzer, HIGH);
delay(2000);
if(bajaw < 3){
digitalWrite(buzzer, LOW);
}
lcd.clear();
lcd.print(“LOCKED”);
digitalWrite(red, HIGH);
}
count=1; // Resetting the counter variable
}
/ Condition to Unlock Ends*/

/* Condition to Change Password Begins /
else if(temp_press==‘C’)
{
checkPassword();
if(flag==0)
{
lcd.setCursor(0,0);
lcd.print(“ENTER NEW PASS”);
key_id=0;
entry=0;
}else{
lcd.setCursor(0,0);
lcd.print(“WRONG PASSWORD”);
bajaw=bajaw+1;
digitalWrite(buzzer, HIGH);
delay(1000);
if(bajaw < 3){
digitalWrite(buzzer, LOW);
}
}
count=1; // Resetting the counter variable
}
/ Condition to Change Password Ends /
/ Condition to LOCK Begins*/

else if(temp_press==‘B’)
{
lcd.setCursor(0,0);
lcd.print(“LOCKED”);
digitalWrite(red, HIGH);
count=1; // Resetting the counter variable
}
/* Condition to LOCK Ends*/
}
/* Actions on Key Press Ends*/
}
/* Main Program Ends /
void SetPassword() // Subroutine to SET User Defined Password
{
keyscan();_
if(key_id==1)
_ {_
if(temp_press==‘A’||temp_press==‘C’||temp_press==‘B’) // Condition to Check for an Invalid Keypress*

* {*
* lcd.setCursor(0,0);*
* lcd.print(“INVALID KEYS”);*
* digitalWrite(buzzer, HIGH);*
* delay(500);*
* digitalWrite(buzzer, LOW);*
* entry=0;*
* }*
* else*
* {*
* pass[entry]=temp_press;
_ int entryy = entry + 1;*_

* EEPROM.write(entryy, temp_press);
_ }
}_
key_id=0;
_ if(entry==5)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“PASSWORD SET & LOCKED”);
digitalWrite(red, HIGH);
delay(100);
}
} // Subroutine to SET Password ENDS*_

* char keyscan()// Subroutine to Scan Keypad for a Keypress*
* {*
* for(i=0; i<=3; i++)*
* {*
* digitalWrite(row[0],HIGH);*
* digitalWrite(row[1],HIGH);*
* digitalWrite(row[2],HIGH);*
* digitalWrite(row[3],HIGH);*
_ digitalWrite(row*,LOW);
for(j=0; j<=3; j++)
{_

col_scan=digitalRead(col[j]);
if(col_scan==LOW)
_ {_
key_id=1;
_ entry++;_
temp_press=keypress(i,j);
lcd.setCursor(lcd_count++,1);
lcd.print(temp_press);
if(temp_press==‘A’||temp_press==‘C’||temp_press==‘B’)
_ {_
lcd_count=0;
_ lcd.clear();
}
delay(300);
break;
}}
}}// Subroutine to Scan Keypress Ends*

* char keypress(int i, int j) // Subroutine to Identify the value of Key pressed*
* {
if(i==0&&j==0)
{
return(‘1’);
}
if(i==0&&j==1)
{
return(‘2’);
}
if(i==0&&j==2)
{
return(‘3’);
}
if(i==0&&j==3)
{
return(‘A’);
}
if(i==1&&j==0)
{
return(‘4’);
}
if(i==1&&j==1)
{
return(‘5’);
}
if(i==1&&j==2)
{
return(‘6’);
}
if(i==1&&j==3)
{
return(‘B’);
}
if(i==2&&j==0)
{
return(‘7’);
}
if(i==2&&j==1)
{
return(‘8’);
}
if(i==2&&j==2)
{
return(‘9’);
}
if(i==2&&j==3)
{
return(‘C’);
}
if(i==3&&j==0)
{
return(’’);

* }
if(i==3&&j==1)
{
return(‘0’);
}
if(i==3&&j==2) {
return(’#’); }
if(i==3&&j==3) {
return(‘D’); }
} // Subroutine to identify Keypress Ends*

void checkPassword() // Subroutine to Check User Input Data with SET Password
{
flag=0;
for(i=1;i<=5&&flag==0;i++) {
* Serial.print(EEPROM.read(i), DEC);_
if(check==pass _|| check==EEPROM.read(i)) {
flag=0; }
else {flag=1; }
}
} // Subroutine to check password ends*_

shanjedul:
I have the same problem… my code is…


Please edit your post and use code tags so we don’t have to look at code that is partially in italics. There is a sticky somewhere how to do it.

Ah, got it: http://forum.arduino.cc/index.php/topic,148850.0.html, specifically point 7.