Keypad and password

it would like to use a keypad to start a led but it doesn't work,
when the password isn't correct the led is on. and when the password is correct its also on i already checked the tutorials but i can't get fuirther then this any help would be nice

#include <Keypad.h>

int ledgroen = 3; // green led
int ledorangje = 4; // orange led
int ledrood = 13; // red led
int doorState = 2;
char PW[] = "1234";
char CANCEL_KEY = '#';
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{

pinMode(ledgroen, OUTPUT);
pinMode(ledorangje, OUTPUT);
pinMode(ledrood, OUTPUT);
Serial.begin(9600);
}

void loop()
{

switch (doorState) {
case 1:
digitalWrite(ledrood, LOW);
delay(500);
digitalWrite(ledgroen, HIGH);
break;
case 2:
digitalWrite(ledrood, HIGH);
break;
}


char PW[] = "12345";
char CANCEL_KEY = '
';
// returns true when all PW keys are pressed in sequence
// returns false when number of keys in pW pressed but don't exactly match the PW
// CANCEL_KEY erases all previous digits input
char key = kpd.getKey();

if(key) // same as if(key != NO_KEY)
{

switch (key)
{
case '#':
{
char key = kpd.getKey();

doorState= 1;
}
// digitalWrite(ledrood, LOW);
// digitalWrite(ledgroen, HIGH);
break;
case '*':

Serial.print("CLEAR");
digitalWrite(ledrood, HIGH);
delay (1000);
digitalWrite(ledrood, LOW);
break;
default:
Serial.print(key);
digitalWrite(ledorangje, HIGH);
delay (1000);
digitalWrite(ledorangje, LOW);
}
}

}

Your variable doorState is not being initialised. Also it is only ever set to a value of one. This is where you are going wrong.

i already tryed many other tings like this

case '#':
{

char key = kpd.getKey();
if(key == 12345) // and many other tings
{

doorState= 1;
}
}
// digitalWrite(ledrood, LOW);
// digitalWrite(ledgroen, HIGH);
break;

You've declared PW twice, but you don't refer to either instance anywhere, nor do you have any kind of index mechanism to decide which key you are currently meant to be matching.

could you give me a example i'm not 100 % sure what you mean

There is nowhere in your code where you compare a keycode against any member of the array PW.

You need to set an index to zero, see it the current keycode == PW[index].
If it is, you increment "index", if it doesn't you set a flag to say invalid code.
Don't simply give up and light the RED LED when you get the first wrong code, 'cos that's a really simple way to crack your code :smiley:
The rest is left as an exercise for the reader

take it long to write it else it would be nice if you could help me a hand with the code but thanks anyway for the replays

what about this he count the numbers but he says 0 matchcount

#include <Keypad.h>

int ledgroen = 3;
int ledorangje = 4;
int ledrood = 13;
int doorState = 2;
char PW[] = "12345";
char CANCEL_KEY = '';
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'
','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{

pinMode(ledgroen, OUTPUT);
pinMode(ledorangje, OUTPUT);
pinMode(ledrood, OUTPUT);
Serial.begin(9600);
}

void loop()
{

switch (doorState) {
case 1:
delay (500);
digitalWrite(ledrood, LOW);
delay(500);
digitalWrite(ledgroen, HIGH);
break;
case 2:
digitalWrite(ledrood, HIGH);
break;
}
// returns true when all PW keys are pressed in sequence
// returns false when number of keys in pW pressed but don't exactly match the PW
// CANCEL_KEY erases all previous digits input
char key = kpd.getKey();


if(key)
{

switch (key)
{
case '#':
{
Serial.print("#");
int count; // how many keys we have
int matchCount; // how many keys match

for( count=0, matchCount=0; count < sizeof(PW)-1; count++){
char key;// = kpd.get_key();
if(key != '\0'){
if(key == PW[count++])
matchCount++;
else if(key == CANCEL_KEY)
count = matchCount = 0;
}
}
// here when the same number of keys pressed as characters in the PW
Serial.print(matchCount);
if( matchCount == count)
doorState= 1;
else
doorState= 2;
}

break;
case '*':
break;
default:
Serial.print(key);
digitalWrite(ledorangje, HIGH);
delay (1000);
digitalWrite(ledorangje, LOW);
}
}

}

You need to add maybe another 10 lines (and chop out the stuff you don't need) and you're there.

Make the last character of PW a '#'.
Declare a variable "index" and set it to zero.
In the '*' case, set index to zero.

In the default case, check to see if "key == PW [index]", if it does, increment index (but make sure it doesn't point past the end of PW).
In the '#' case, if index is pointing to a '#' character, you're there, so open the door.

[EDIT] The last attempt can't ever work - go back to your original posted code.
Please use the # icon to post code.

I have actually made a Password library for this exact purpose.

I think it might help you clean up you code:
http://www.arduino.cc/playground/Code/Password

Example 'HelloPassword':

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

Password password = Password( "1234" );

void setup(){
  Serial.begin(9600);
 
  password.append('1');   //add 1 to the guessed password
  password.append('2');   //add 2 to the guessed password
  password << '3' << '4'; //add 3 and 4 to the guessed password
  Serial.println( password.evaluate() ? "true":"false" ); //should print true, since 1234 == 1234
  
  password.reset(); //reset the guessed password to NULL
  Serial.println( password.evaluate() ? "true":"false" ); //should print false, since 1234 != NULL
  
  password.set("qwerty"); //set target password to qwerty
  Serial.println( password.is("qwerty") ? "true":"false" ); //should print true, since qwerty == qwerty
  Serial.println( password == "qwirty" ? "true":"false" ); //should print false, since qwerty != qwirty
}

void loop(){/*nothing to loop*/}

And here is the previously mentioned application:

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

Password password = Password( "1234" );

const byte rows = 4; //four rows
const byte cols = 4; //four columns
const byte rowPins[] = {2,3,4,5}; //connect to the row pinouts of the keypad
const byte colPins[] = {6,7,8,9}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad(rowPins,colPins,rows,cols);

const byte ledPin = 13;

void setup(){
pinMode(ledPin, OUTPUT); // sets the digital pin as output
digitalWrite(ledPin, LOW); // sets the LED off
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 (eKey){
case '*': guessPassword(); break;
case '#': password.reset(); break;
default:
password.append(eKey);
}
}

void guessPassword(){
if (password.evaluate()){
digitalWrite(ledPin,HIGH);
}else{
digitalWrite(ledPin,LOW);
}
}

ok this is my code now.
But when i enter a number he dont change anymore so once i pressed 1 he stay showing 1 what ever i press

#include <Keypad.h>

int ledgroen = 3;
int ledorangje = 4;
int ledrood = 13;
int doorState = 2;
int index= 0;
char PW[] = "12345#";
char CANCEL_KEY = '';
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'
','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{

pinMode(ledgroen, OUTPUT);
pinMode(ledorangje, OUTPUT);
pinMode(ledrood, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char key = kpd.getKey();

switch (doorState) {
case 1:
delay (500);
digitalWrite(ledrood, LOW);
delay(500);
digitalWrite(ledgroen, HIGH);
break;
case 2:
digitalWrite(ledrood, HIGH);
break;
}
// returns true when all PW keys are pressed in sequence
// returns false when number of keys in pW pressed but don't exactly match the PW
// CANCEL_KEY erases all previous digits input
if(key)
{

switch (key)
{
case '#':
{
Serial.print("#");
int count; // how many keys we have
int matchCount; // how many keys match

for( count=0, matchCount=0; count < sizeof(PW)-1; count++){
char key;// = kpd.get_key();
if(key != '\0'){
if(key == PW[count++])
matchCount++;
else if(key == CANCEL_KEY)
count = matchCount = 0;
}
}
// here when the same number of keys pressed as characters in the PW
Serial.print(matchCount);
if( matchCount == count)
doorState= 1;
else
doorState= 2;
}

break;
case '*':
index= 0;
break;
default:

char key = kpd.getKey();

Serial.print(PW[index]);
if(key == PW[index])
{
doorState= 1;
}
doorState= 2;


}
}

}

nice im gonn

nice im gonna try it out

Great it changed the code abit and it works but only have 1 problem when 1 press 1 key he send it 2 times

You probably need to add some debounce time. Try adding this line to setup:

keypad.setDebounceTime(250);

when i do that he just delay the second press but he still send it 2 times