I'm currently developing a password protected "lock" (it really code be used to do anything). I've made it so that you can set a new password on the fly, without a computer. I'm fairly new to Arduino, with a limited background in programming, so any pointers would be appreciated!
The program work as expected, but I would like to add some more functionality in the near future.
Here's a list of feature I'd like to implement:
-Maximum code of 8 characters
-Ability to set more than one password
-Select a password slot (1-9) and set it
-If a password already exists in a slot, the user would be required to enter the previous password first or a Master password would have to be entered prior to setting a new one.
Like I said, I'm fairly new to Arduino so any improvements or comments would be great.
#include <Keypad.h>
#include <Password.h>
/*
To enter a code, punch in a four digit number and press '*'.
If the password is correct an LED (or anything) will be turned on
for a certain amount of time. To set a code, press on hold the '#'
key until an LED starts to flash, enter in a desired 4 digit code
and press '*' to set. Press and hold '#' to exit programming mode.
At anytime when entering a code press '#' to clear the current entry.
- ThatRusty 2012
*/
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
// Define the keymap
char PadKeys[ROWS][COLS] = {
{ '1','2','3', 'D' },
{ '4','5','6' },
{ '7','8','9', 'S' },
{ '*','0','#', 'C' }
};
boolean setPass = false; // Start with the entry keypad.
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6,9}; //connect to the column pinouts of the keypad
// Create "two" new keypads, one is a entry pad and the other is a programming pad.
Keypad entryPad( makeKeymap(PadKeys), rowPins, colPins, sizeof(rowPins), sizeof(colPins) );
Keypad setPad( makeKeymap(PadKeys), rowPins, colPins, sizeof(rowPins), sizeof(colPins) );
unsigned long startTime;
const byte ledPin = 11;// Programming LED, flashes when in program mode
const byte ledPin1 = 13; //Green LED, correct code + key press
const byte ledPin2 = 12; //red LED, Error
Password password = Password( "5555" ); //Default password 1
Password password2 = Password( "4321" ); //Default password 2
char Password2[5]; //Array used to set password #2
char PasswordArray[5]; //General entry array, used to store key presses
int i = 0; //counter
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode (ledPin1, OUTPUT);
pinMode(ledPin2,OUTPUT);
digitalWrite(ledPin, LOW); // start programming LED off
//Set up keypads
setPad.begin( makeKeymap(PadKeys) );
entryPad.begin( makeKeymap(PadKeys) );
setPad.addEventListener(keypadEvent_set); // Add an event listener for programming mode
setPad.setHoldTime(1000); // Set how long a key must be held before a hold key event
entryPad.addEventListener(keypadEvent_entry); // Add an event listener for entry mode
entryPad.setHoldTime(1000); // Set how long a key must be held before a hold key event
entryPad.setDebounceTime(200);
setPad.setDebounceTime(200);
}
char key; //Define key button variable
void loop() {
if( setPass ) //if setpass == true, get key presses from the programming key pad
key = setPad.getKey( );
else
key = entryPad.getKey( ); //if setpass == false, get key presses from the entry key pad
if (setPass && millis()-startTime>100) { // Flash the programming LED when in program mode
digitalWrite(ledPin,!digitalRead(ledPin));
startTime = millis();
}
}
static byte kpadState; //Define variable to check key state (PRESSED, HOLD...)
// Take care of some special events.
void keypadEvent_set(KeypadEvent key) {
// in here when in program mode.
kpadState = setPad.getState( );
swOnState( key ); //send swOnState function what key was pressed
} // end set keypad events
void keypadEvent_entry( KeypadEvent key ) {
// in here when using the entry keypad
kpadState = entryPad.getState( );
swOnState( key ); //send swOnState function what key was pressed
} // end entry keypad events
void swOnState( char key ) {
switch( kpadState ) {
case PRESSED: //If a key was pressed
blink(ledPin1, 50); //Blink LED when key is pressed, send which LED and how long of a delay
if (setPass) { // In program mode so we're using the programming keymap.
switch (key){
case '*': setPassword(); //After you punch in you new password press '*' to set it
break;
case '#': resetPasswordArray(); //Clear the Password array
Serial.println("Clear");
default:
if (key != '#'){ //if the key pressed was not '#' , append key to entry array
PasswordArray[i] = key; i++;} //increment counter
if (key != '#') {Serial.print(key);} //Print out what key was pressed
}
}else{ //In entry mode, use entry keypad
//Code Entry goes here
switch(key){
case '*': checkPassword();
break;
case '#': resetPasswordArray();
Serial.println();
Serial.println("Clear");
default:
if (key != '#'){ //if the key pressed was not '#' , append key to entry array
PasswordArray[i] = key; i++;} //increment counter
if (key != '#') {Serial.print(key);} //Print out what key was pressed
}
}
break;
case HOLD:
if (key == '#') { //If '#' key was held toggle keypads
if (setPass == true) { // We are currently using a keymap with letters
setPass = false; // Now we want a keymap with numbers.
digitalWrite(ledPin, LOW);
}
else { //Currently using the entry keypad
setPass = true; // Now we want to use the programming keypad
}
}
break;
} // end switch-case
}// end switch on state function
void checkPassword() {
Serial.println();
if (i>3 && i<5){ //Only accept passwords that are 4 characters
if (password.is(PasswordArray)){ //check punched in code againsted password
Serial.println("Access Granted (1)");
blink (ledPin1, 700); //Blink LED
}else if (password2.is(PasswordArray)){ //If punched in code wasn't the first password check the second
Serial.println("Access Granted (2)");
blink (ledPin1, 700);
} else {
Serial.println("Access Denied");
blink(ledPin2, 700);
}
}else{
Serial.println("Your password must be 4 characters.");
blink(ledPin2, 700);
}
i=0; //reset counter so another code can be entered
resetPasswordArray();
password.reset();
password2.reset();
}
void setPassword(){
Serial.println();
if (i>3 && i<5) { //Only accept passwords that are 4 characters
for (i=0;i<4;i++){
Password2[i] = PasswordArray[i]; //Set Password2 array to characters in entry array
}
password2.set(Password2); //Set password2 to the punched in code
Serial.println("New password has been set...");
}else{
Serial.println("Your password must be 4 characters.");
blink(ledPin, 700);
}
i=0; //reset counter
resetPasswordArray();
// User must exit program mode by holding the '#' key
}
void resetPasswordArray() { //Clear entry array
for (i=0;i<5;i++){
PasswordArray[i] = '\0'; //set all characters in the entry array to null
}
i=0; //reset counter
}
void blink (const byte LED, int DELAY){
digitalWrite(LED, !digitalRead(LED));
delay (DELAY);
digitalWrite(LED, !digitalRead(LED));
}