You, the evil genius wouldn't rest easy till you are sure that all of your creations are locked away somewhere safe, now would you? Well, this project is exactly about that! I'm gonna show you how to make your very own keypad security lock using an arduino board and a bit of cunning.
Parts Needed
- Arduino Uno or a clone
- Two 270 Ohm metal film resistors
- A Green 5mm LED
- A Red 5mm LED
- A 4X3 Matrix Keypad
Understanding the Concept


Software
You may think that we would just write a software which interprets which key is pressed by receiving signals from the pins and the game would be over, but this is not the case. The key doesn't simply go from being open to being closed when you press it, but opens or closes many times. This is called bouncing and we need to incorporate some mechanism which overcomes bouncing. The mechanism will be called debouncer.
No no, you need to fret! Because fortunately for us, there exists a library that handles this for us. This is the link to the library. To install it, just extract from the zip folder, and paste the extracted folder in C:\Program Files\Arduino\Arduino-0017\hardware\libraries or where ever you have installed the arduino IDE. Check that the library is installed by restarting the IDE, starting a new sketch, and choosing the menu option Sketch
Import Library | Keypad. This will insert the text “#include <Keypad.h>” on the top of the file. The code that I'm going to show you is just an example. You may have to change your int pins to agree with the layout of your keypad.
Here's the code:
#include <Keypad.h>
char* secretCode = "1234";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 9;
int greenPin = 8;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
if (key == '*' || key == '#')
{
position = 0;
setLocked(true);
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4)
{
setLocked(false);
}
delay(500);
}
void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
}
The loop function checks for a key press. If the key pressed is # or *, it sets the position variable to 0. If, on the other hand, the key pressed is one of the numerals, it checks to see if it is the next key expected (secretCode[position]) is the key just pressed, and if it is, it increments position by one. Finally, the loop checks to see if position is 4, and if it is, it sets the LEDs to their unlocked state. Then it waits a second, and turns the red light green. Load the completed sketch for Project 10 from your Arduino Sketchbook and download it to the board.
If you have trouble getting this to work, it is most likely a problem with the pin layout on your keypad. So persevere with the multimeter to map out the pin connections.
And it goes without saying that if you have any problems, any at all, just mail me or comment.
OVER AND OUT.
0 comments:
Post a Comment
Do Comment on Anything you like or dont like!