Problem With Multiple Passwords

Hi there, I’m trying to make a door lock with multiple passwords. Right now I only have the password part programmed in. I can only get on password working though. Here is the first code that i tried using:

#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip

Password password = Password( "1234" );
Password password2 = Password( "4568" );

const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'*','#','9'},
{'7','8','0'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 53, 51, 49, 47 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 45, 43, 41 };


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

void setup(){

Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
  case PRESSED:
    Serial.print("Pressed: ");
    Serial.println(eKey);
    switch (eKey){
     case '*':
        checkPassword(); 
        checkPassword2();
        break;
     case '#':
        password.reset();
        password2.reset();
        break;
     default:
        password.append(eKey);
        password2.append(eKey);
  }
}
}

void checkPassword(){
if (password.evaluate()){
  Serial.println("code 1 success");

}else {
  Serial.println("Wrong");
  //add code to run if it did not work
}
}

void checkPassword2(){
if (password2.evaluate()){
  Serial.println("code 2 success");

}else {
  Serial.println("Wrong");
  //add code to run if it did not work
}
}

Whenever this one runs, the first code returns:
code 1 success
Wrong
The second code returns:
Wrong
Wrong

I’m not sure what to do with this so any help would be appreciated.
Thanks!

Looks like you are not clearing the password after you evaluate. If you press "1234*" and then "1234*" again your password will contain "12341234" the second time and that will not match "1234". Make this change:

     case '*':
        checkPassword(); 
        checkPassword2();
        //  Comment out the break; so the passwords get reset after checking.
     case '#':
        password.reset();
        password2.reset();
        break;

i dont know if you still need this but i was struggling with the same question and finally came up with an answer, so i will post it in case someone else need it as well.

what you were missing is that when you do the defaul password.append you need to do as well password1.append too so the keys that you entered are assigned to the password variable and the password1 variable as well and as the previous guy said you need to reset both too

#include <Password.h>
#include <Keypad.h>

Password password = Password( “1234” );
Password password1 = Password( “4567” );

const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

byte rowPins[ROWS] = { 9, 8, 7, 6 };
byte colPins[COLS] = { 5, 4, 3, 2 };

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

void setup(){
Serial.begin(9600);
delay(200);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

keypad.addEventListener(keypadEvent);
}

void loop(){
keypad.getKey();

}

void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:

Serial.print(“Enter:”);
Serial.println(eKey);
delay(10);

Serial.write(254);

switch (eKey){
case ‘A’: checkPassword(); delay(1); break;
case ‘B’: checkPassword1(); delay(1); break;
case ‘#’: password.reset(); password1.reset(); delay(1); break;

default: password1.append(eKey); password.append(eKey); delay(1);

}
}
}

void checkPassword(){

if (password.evaluate()){

Serial.println(“Aceptado… Arracando”);
Serial.write(254);delay(10);
digitalWrite(11, HIGH);
delay(200);
digitalWrite(11, LOW);

}else{
Serial.println(“Denegado”);
Serial.write(254);
delay(10);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW);
}
password.reset(); password1.reset();
}

void checkPassword1(){

if (password1.evaluate()){

Serial.println(“Aceptado… GPS ACTIVADO”);
Serial.write(254);delay(10);
digitalWrite(11, HIGH);
delay(200);
digitalWrite(11, LOW);

}else{
Serial.println(“Denegado”);
Serial.write(254);
delay(10);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW);
}
password.reset(); password1.reset();
}

You should NOT be unconditionally resetting both passwords in both functions.

@PaulS in MY case i DO

@Estebanchacon:

I used your code with some slight tweaks and it worked perfectly. Instead of both passwords being checked in ekey, I sent put checkpassword1() at the end of checkpassword(). Both passwords are using "*" for a check and I was having problems with it checking the second password, thanks.