Pages: [1]   Go Down
Author Topic: Help rewriting sketch: keypad-lock  (Read 616 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
default:
      attempt[z]=key;
      z++;

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
bool locked = true;

In the default case, add some code:
Code:
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.
Logged

Pages: [1]   Go Up
Jump to: