Hello everyone, I am in need of some insight on how to get this keypad lock to meet my needs. Currently, the following code will cause the servo to unlock my door when the correct code is entered followed by the * key. After it unlocks, it waits 10 seconds and then locks back again. The LED in the code is an RGB LED that lights green when locked and red when unlocked.
I would like to remove the time lock function and have it stay unlocked until the # key is pressed. For example, you enter the code correctly and it unlocks, and it stays that way until I press # instead of just automatically locking after 10 seconds has elapsed.
The servo moves to 80 degrees to unlock and 0 degrees to lock.
password.reset() is called after entering the password right or wrong to purge the memory of whatever password was entered so that you can try again.
I'm not asking anyone to do this for me, I am simply asking for some advice on what snippets of code I can use to get this to work the way I want to. I have tried removing the delay after the correct code is entered so that it moves to else, but it just causes the servo to not move at all and the LED to be set to red.
Any help is greatly appreciated, see the code below.
#include <Password.h> //http://playground.arduino.cc/uploads/Code/Password.zip //tells to use password library
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip //tells to use keypad library
#include <Servo.h> //tells to use servo library
Servo myservo; //declares servo
Password password = Password( "0000" ); //password to unlock, can be changed
const byte ROWS = 4; // Four rows
const byte COLS = 4; // 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] = { 5, 4, 3 };
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
Serial.write(254);
Serial.write(0x01);
delay(200);
pinMode(11, OUTPUT); //green light
pinMode(12, OUTPUT); //red light
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
myservo.attach(13); //servo on digital pin 9 //servo
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop(){
keypad.getKey();
myservo.write(20);
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Enter:");
Serial.println(eKey);
delay(10);
Serial.write(254);
switch (eKey){
case '*': checkPassword(); delay(1); break;
default: password.append(eKey); delay(1);
}
}
}
void checkPassword(){
if (password.evaluate()){ //if password is right open
Serial.println("Accepted");
Serial.write(254);delay(10);
//Add code to run if it works
myservo.write(80); //deg
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
delay(10000);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
password.reset();
}else{
Serial.println("Denied"); //if passwords wrong keep locked
Serial.write(254);delay(10);
//add code to run if it did not work
myservo.write(0);
password.reset();
}
}