multiple codes with keypad

i have a sparkfun keypad and want to programme it to turn various outputs when certain codes are inputed, how do i write sketch for multiple passwords?? so far i can only get it to accept one

so far i can only get it to accept one

And THAT code looks like?

here’s the code, i’m just using an led as output for the moment. at the minute when i enter the code 001 and press # the led turns on and if i clear the code with * led turns off, but i want to add another code say 002# to turn on another seperate led

/*
|| Simple Password Entry Using Matrix Keypad
|| 4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.com
||
*/

//* is to validate password
//# is to reset password attempt

/////////////////////////////////////////////////////////////////

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

Password password = Password ( “001” );
int ledPin = 13;
const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};

byte rowPins[ROWS] = { 5,4,3,2};// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 8,7,6, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.

// 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(); break;
case ‘*’: password.reset(); digitalWrite(ledPin, LOW); break;
default: password.append(eKey);
}
}
}

void checkPassword(){
if (password.evaluate()){
digitalWrite(ledPin, HIGH);

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

Password password = Password ( "001" );

This object defines a valid password.

     default: password.append(eKey);

This code appends a keystroke to the storage area defined for that object.

     case '*': password.reset(); digitalWrite(ledPin, LOW); break;

This code (poor style! One statement per line! (You’ll see why soon)) resets the storage area for that object.

  if (password.evaluate()){

This code checks whether the keystrokes in the storage area match the valid string defined for the object.

If you want multiple passwords, you’ll need multiple objects (instances of the Password class). You’ll need to append the keystroke to each object, reset each object, and evaluate each object.

ok here’s my new code when input the first password(001) it works perfect but if i try input password 2(002) nothing happens at all

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

Password password = Password( “001” );
Password password2 = Password( “002” );
int ledPin1 = 13;
int ledPin2 = 12;
const byte ROWS = 4; // 4 rows
const byte COLS = 3; // 3 columns
// Define the Keymap
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};

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

// 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()){
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(2000);
digitalWrite(ledPin2, LOW);
}
}
void checkPassword2(){
if (password2.evaluate()){
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
delay(2000);
digitalWrite(ledPin1, LOW);
}
}

apologises i thought i had my keypad setup to have the # key as the enter rather than the * key, above code works fine

above code works fine

Maybe, but it doesn't look good.

Password password = Password( "001" );
Password password2 = Password( "002" );

If you are going to append a number to one instance of a variable name, you should append a number to all instances.

You only need one function to check the passwords. You can check both of them in the same function.

collyd21:
i have a sparkfun keypad and want to programme it to turn various outputs when certain codes are inputed, how do i write sketch for multiple passwords?? so far i can only get it to accept one

I hope this works for you

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

Password password = Password( “001” );
Password password2 = Password( “002” );
int ledPin1 = 13;
int ledPin2 = 12;
const byte ROWS = 4; // 4 rows
const byte COLS = 3; // 3 columns
// Define the Keymap
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};

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

// 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;
default:
password.append(eKey);
password2.append(eKey);
}
}
}

void checkPassword(){
if (password.evaluate()){
Serial.print("good ");
digitalWrite(ledPin1,!digitalRead(ledPin1));
delay(1000);
password.reset();
password2.reset();
}
}
void checkPassword2(){
if (password2.evaluate()){
Serial.print("good ");
digitalWrite(ledPin1,!digitalRead(ledPin1));
delay(1000);
password.reset();
password2.reset();
}
}