I have a school project to make a key lock. What it should be doing is take a password, see if its correct, and if it is, turn on the green LED(or open the door). If its not, turn on the red LED and add 1 to "i" variable, so it counts mistakes. If it reaches 3 mistakes, it waits for 10 seconds, then the master code is needed, which can be enterd 3 times as well(it has its own variable, "p"), and if it was wrong, the program stops.
It all works fine unless I add that second part of the program, where master code is needed.
Could you help me figure out what the problem is? And if possible, I would need it asap, because the deadline is really close. Thank you in advance.
//* is to validate password
//# is to reset password attempt
/////////////////////////////////////////////////////////////////
#include <Password.h>
#include <Keypad.h>
Password password = Password( "0721" );
Password epassword = Password( "123" );
const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
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 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5,4,3,2, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int GLed = 31;
int RLed = 33;
int BLed = 35;
int i = 1;
int p = 1;
void setup()
{
pinMode(GLed, OUTPUT);
pinMode(RLed, OUTPUT);
pinMode(BLed, OUTPUT);
Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
keypad.addEventListener(kpdEvent);
}
void loop(){
keypad.getKey();
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
if(i == 1 || i == 2 || i == 3)
{
switch (keypad.getState()){
case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
digitalWrite(BLed, HIGH);
delay(100);
digitalWrite(BLed, LOW);
switch (eKey){
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}
if(i == 4)
{delay(10000);
digitalWrite(BLed, HIGH);
delay(1000);
digitalWrite(BLed, LOW);
}
}
void checkPassword(){
if (password.evaluate()){
Serial.println("Success");
digitalWrite(GLed, HIGH);
delay(1000);
digitalWrite(GLed, LOW);
password.reset();
i = 1;
p = 1;
//Add code to run if it works
}else{
Serial.println("Wrong");
digitalWrite(RLed, HIGH);
delay(1000);
digitalWrite(RLed, LOW);
password.reset();
i = i+1;
p = p+1;
}
}
//NOTICE
//If everyhing below is removed, program works. "keypad.addEventListener(kpdEvent);" line also needs to be removed in order for program to work
void kpdEvent(KeypadEvent Key){
if(p == 4 || p == 5 || p == 6)
{
switch (keypad.getState()){
case PRESSED:
Serial.print("Pressed: ");
Serial.println(Key);
digitalWrite(BLed, HIGH);
delay(100);
digitalWrite(BLed, LOW);
switch (Key){
case '*': checkPass(); break;
case '#': epassword.reset(); break;
default: epassword.append(Key);
}
}
}
if (p == 7)
{
while(1);
}
}
void checkPass(){
if( (epassword.evaluate())){
Serial.println("Success");
digitalWrite(GLed, HIGH);
delay(1000);
digitalWrite(GLed, LOW);
epassword.reset();
i = 1;
p = 1;
//Add code to run if it works
}else{
Serial.println("Wrong");
digitalWrite(RLed, HIGH);
delay(1000);
digitalWrite(RLed, LOW);
epassword.reset();
p = p+1;
i = i+1;
}
}