Introduction: Keypad Combiation

Picture of Keypad Combiation

Hello makers. Today i will be showing you how to make a keypad combination lock. This keypad has a clear button as well as the ability to change the combination without reprogramming the Arduino. After testing, this combination lock is foul-proof, unless you break the circuit (DUH!!). So let's get building.

Step 1: Parts and Libraries

Picture of Parts and Libraries

To make this project, you need:

- An Arduino.

- An LCD display(this code is for a 16*2 LCD).

- A 4x4 Keypad

- Potentiometer(i used a 50k pot)

- Green and Red LED(Or you can program the output to do anything)

- 220 or 270 ohm resistor.

For the libraries, you need:

- Keypad Library: http://playground.arduino.cc/Code/Keypad

-LCD library: Included with the IDE.

Step 2: Circuitry

Picture of Circuitry

Just make the circuit by following the schematic above. There is also a download for the Fritzing file if you can't comprehend the schematic from the image.

Step 3: Code and Customizing It

Picture of Code and Customizing It

Here is the code for this project. The default combination is 1-2-3-4. To customize this from the code, you edit the array called "rightnumber". You need to put it the number in decimal ASCII. This is because of how my code and the keypad library works. To edit the amount of numbers in the combination, you edit the variable called "combinationlenght". Just make sure your combination length isn't over 14 digits, as the code isn't programmed to handle such length. Also make sure the combination length provided matches with the amount of number in the "rightnumber" array, as not doing so will mess up the program. If you want custom actions for the right and wrong combination, there is a void at the end of the code called "rightaction" and "wrongaction".

Step 4: Usage

You type the combination by pressing the key one by one like you would in a normal lock. A * should pop up every key press. If it doesn't press that key again. To clear the written number(if you got something wrong), press the D key. If you get the combination right or wrong, press D to go the the home screen and be able to type the combination again. This will also turn off the LED. If you get the combination right, you can press C to change the combination without having to reprogram the Arduino. Simply, after pressing C, type in your combination. Again you can use the D key to clear the written number if you typed in the wrong thing. One the number of key presses reaches the combination length, it will ask you if your sure. To say yes(changing the combination to the one you inputed) and return to the home screen, press A. If you don't want to change it(maybe you accidentally pressed the wring key), press B. This will return to the right combination screen, meaning you have the press C to change the combination or press D to go to the home screen.

Step 5: The End and Future Plans

If you followed the steps correctly, you have successfully made a combination lock. For the future of this project, if there is enough demand or if i just want to it, i will make V2 with more features(maybe add a remote control access i don't know). If you guys find a bug in this code, please tell inform me so i could fix it. If you guys want i could potentially make a video explaining how this project works and the code(how it works and how to customize it). Last, if you guys have an idea for a future projects, leave them bellow. Have fun

Comments

devicemodder made it! (author)2016-08-28

I modified the code a bit to work with 4x3 keypads. I also added an rgb led that has 3 states.

Red= Access Denied

Green = Access Granted

Blue = Code change

I also added an unlock function with a transistor, as seen turning on the board on the right.

Here's a video of it in action. https://www.youtube.com/watch?v=BzLH9g0wG1E

The * key is used to change the code and accept. The # sign is used for home. or back if at the code change screen.

//Begin code: #include<Keypad.h>
#include <LiquidCrystal.h>

const int combinationlenght = 6;
int rightnumber[combinationlenght] = {49, 50, 51, 52, 53, 54};//this codes for 1,2,3,4 in ASCII. Had to use it because the library stores the input in decimal ASCII intead of a number.


int newrightnumber[combinationlenght];
char number[combinationlenght];
int right = 0;
int unlock = 2;
int green = A1;
int red = A2;
int blue = A3;
int newcomb = 0;
byte fulllcd[8] = {B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111,};
int newfinalcomb = 0;
int kposition = 0;
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(13,12,11,10,1,0);
void setup() {
off();
// put your setup code here, to run once:
// Serial.begin(9600);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
pinMode(2,OUTPUT);
lcd.createChar(0, fulllcd);
lcd.begin(16,2);
lcdstartup();
}

void loop() {
// put your main code here, to run repeatedly:
char k = keypad.getKey();
if(k != 0){ //if you press something
if(k == 68){
kposition = 0;
lcdstartup();

}
else{
number[kposition] = k; //save number in array
kposition++; //add to the combination digit position
lcd.setCursor(kposition - 1,1);
lcd.print("*"); //writes a * each time you enter in a charcter
delay(50); //delay just cause i can
if(kposition == combinationlenght){ //checks if we pressed 4 numbers
kposition = 0; //sets array write position to 0
for(int g=0;g<combinationlenght;g++){//if each digit is right, add to the variable "right"
if(number[g] == rightnumber[g]){ right++;} //checks if all inputed digits are right
}
if(right == combinationlenght){
rightaction();
rightlcd();
right = 0;
}
else{
wrongaction();
wronglcd();
right = 0;
}
delay(100);
}
}
}
}
void lcdstartup(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Combination :");

}
void lcdenter(){
lcd.clear();
lcd.print("Enter new :");
}
void lcdgranted(){
lcd.clear();
lcd.print("*****ACESS******");
lcd.setCursor(0,1);
lcd.print("****GRANTED*****");
}
void rightlcd(){
lcdgranted();
while(newfinalcomb == 0){
delay(10);
char o = keypad.getKey();
if(o != 0){
if(o == 42){
digitalWrite(green,HIGH);
digitalWrite(unlock,LOW);
lcdenter();
while(newcomb == 0){
enternewcode();}
newcomb = 0;
}
if(o == 35){
newfinalcomb = 1;
digitalWrite(green,HIGH);
digitalWrite(unlock,LOW);
}
}
}
newfinalcomb = 0;
lcd.clear();
lcdstartup();
}
void enternewcode(){\
codeentry();
char newk = keypad.getKey();
if(newk != 0){
newrightnumber[kposition] = newk;
kposition++;
lcd.setCursor(kposition,1);
lcd.print(newk);
if(newk == 42){
lcd.setCursor(0,1);
lcd.print(" ");
kposition = 0;
}
if(kposition == combinationlenght){
lcd.setCursor(kposition + 1,1);
lcd.write(byte(0));
delay(1000);
kposition = 0;
checknewcode();
digitalWrite(blue,HIGH);
}
}
}
void checknewcode(){
lcd.clear();
lcd.print("Are you sure ?");
while(newcomb == 0){
char newc = keypad.getKey();
if(newc != 0){
if(newc == 42){
lcd.clear();
lcd.print("New code");
lcd.setCursor(0,1);
lcd.print("Accepted");
newcomb = 1;
newfinalcomb = 1;
for(int j=0;j<combinationlenght;j++){
rightnumber[j] = newrightnumber[j];
}
kposition = 0;
delay(1500);
}
if(newc == 35){
delay(10);
lcdgranted();
newcomb = 1;
}
}
}
}
void wronglcd(){
lcd.clear();
lcd.print("*****ACESS******");
lcd.setCursor(0,1);
lcd.print("*****DENIED*****");
while(newfinalcomb == 0){
delay(10);
char o = keypad.getKey();
if(o != 0){
if(o == 35){
newfinalcomb = 1;
}
}
}
newfinalcomb = 0;
lcd.clear();
lcdstartup();
digitalWrite(red,HIGH);
}

void rightaction(){
digitalWrite(green,LOW);
digitalWrite(unlock,HIGH);
digitalWrite(red,HIGH);
}
void codeentry(){
digitalWrite(green,HIGH);
digitalWrite(blue,LOW);
digitalWrite(red,HIGH);
}
void wrongaction(){
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
}
void off(){
digitalWrite(green,HIGH);
digitalWrite(blue,HIGH);
digitalWrite(red,HIGH);

}
//End Code

Oh. Very nice. Sorry I've been inactive for a while. I like the addition of the unlock function and the ribbon cable on the LCD.

DIY Hacks and How Tos (author)2015-11-26

Cool way to make a DIY security system

Thanks :)

DIY Hacks and How Tos (author)2015-11-26

Cool way to make a DIY security system