Good afternoon forum
I am working on a simple project, my idea is to create a safe which can only be locked/unlocked with the right key combination. I got my example sketch form here: http://tronixstuff.wordpress.com/2011/10/04/tutorial-arduino-and-numeric-keypads/ I’ve only modified it to work with a keypad with the A, B, C, D symbols.
So this is what i ended up with:
// Example 42.4 - Six-character keypad switch
// http://tronixstuff.wordpress.com/tutorials > chapter 42a
#include "Keypad.h"
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte rowPins[ROWS] = {
5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char PIN[6]={'1','2','3','4','5','6'}; // our secret (!) number
char attempt[6]={0,0,0,0,0,0}; // used for comparison
int z=0;
void setup()
{
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
incorrectPIN();
}
void correctPIN() // do this if correct PIN entered
{
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(12, HIGH);
}
void incorrectPIN() // do this if incorrect PIN entered
{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
}
void checkPIN()
{
int correct=0;
for (int q=0; q<6; q++)
{
if (attempt[q]==PIN[q])
{
correct++;
}
}
if (correct==6)
{
correctPIN();
} else
{
incorrectPIN();
}
for (int zz=0; zz<6; zz++) // wipe attempt
{
attempt[zz]=0;
}
}
void readKeypad()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
switch(key)
{
case '*':
z=0;
break;
case '#':
delay(100); // for extra debounce
checkPIN();
break;
default:
attempt[z]=key;
z++;
}
}
}
void loop()
{
readKeypad();
}
Pin 10 = Green LED (status)
Pin 11 = Red LED (status)
Pin 12 = Relay (to activate solenoid)
The sketch is working just great, but there’s things i would like to change which i can’t figure out… (Newbie) to unluck i have to press #123456* and to lock again press #
What i would like to do:
to unlock, just press 123456
to lock, just press a random button on keypad
I hope you guys will help me rewrite my sketch, to work the way i want it to, that would be wonderful.
I tried messing around with the sketch myself, but didn’t get any good results at all.
Best regards, JohannesTN