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); } } }
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.
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
The rest is left as an exercise for the reader
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.
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
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);
}
}