Go Down

### Topic: Help rewriting sketch: keypad-lock (Read 943 times)previous topic - next topic

#### JohannesTN

##### Dec 29, 2012, 12:15 am
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:

Code: [Select]
`// Example 42.4 - Six-character keypad switch// http://tronixstuff.wordpress.com/tutorials > chapter 42a#include "Keypad.h"const byte ROWS = 4; //four rowsconst byte COLS = 4; //four columnschar 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 keypadbyte colPins[COLS] = {  9, 8, 7, 6}; //connect to the column pinouts of the keypadKeypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );char PIN[6]={'1','2','3','4','5','6'}; // our secret (!) numberchar attempt[6]={0,0,0,0,0,0}; // used for comparisonint 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

#### PaulS

#1
##### Dec 29, 2012, 12:54 am
Quote
to unlock, just press 123456

So, call checkPin() when z has the right value.

Quote
to lock, just press a random button on keypad

How would you distinguish "a random button" from entering a character in the password?

You could have an if test in the default case. If the door is unlocked, lock it and otherwise ignore the key press. Else, the key is to be added to the array.

#### JohannesTN

#2
##### Dec 29, 2012, 11:13 am
Quote
So, call checkPin() when z has the right value.

Fine with me :-) - but where to call it? i need this to be cut out in cardboard please (don't know if you can use that saying on English)

Quote
How would you distinguish "a random button" from entering a character in the password?

Actually i didn't think to much about how to handle that.. If i use your "if test" how hard would i be to implement for a beginner?

Thank you very much, i appreciate your help!

#### marcello.romani

#3
##### Dec 29, 2012, 01:03 pm
Code: [Select]
`default:      attempt[z]=key;      z++;`

What if one enters more than 6 numbers without pressing * or # ?

#### PaulS

#4
##### Dec 29, 2012, 02:34 pm
Quote
Fine with me :-) - but where to call it? i need this to be cut out in cardboard please (don't know if you can use that saying on English)

In the default case, after you've added the 6th character to the array.

Code: [Select]
`default:      if(z <= 5)      {        attempt[z]=key;      }      z++;      if(z == 5)      {         checkPin();      }`
(Takes care of tuxduino's question, too.)

Quote
If i use your "if test" how hard would i be to implement for a beginner?

Depends on how willing you are to try. You need a variable to keep track of whether the door is locked, or not. Since the values are true (it is locked) or false (it is not locked), a boolean is ideal.

Code: [Select]
`bool locked = true;`

In the default case, add some code:
Code: [Select]
`default:   if(locked)   {      // move all the current code here   }   else   {      // Lock the door, and set locked to true   }`

Where you unlock the door, you also need to set locked to false.