Last active
January 3, 2019 11:58
-
-
Save avseng/68324a8225d29883ec772a51efdfb361 to your computer and use it in GitHub Desktop.
Secure Box project with arduno nano, 4x4 keypad, 16x2 LCD and Servo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Keypad.h> | |
#include <LiquidCrystal.h> | |
#include <EEPROM.h> | |
#include <string.h> | |
#include <Servo.h> | |
Servo myservo; | |
int pos = 0; | |
const byte numRows= 4; //number of rows on the keypad | |
const byte numCols= 4; //number of columns on the keypad | |
#define Password_Length 6 | |
char Data[Password_Length]; | |
char Data_Final[Password_Length]; | |
char pass1[Password_Length]; | |
char newPassCode[Password_Length]; | |
char newPassCodeReEntered[Password_Length]; | |
char MasterLocal[Password_Length] = "56238"; // 5 is the number of chars it can hold + the null char = 6 | |
char Master[Password_Length]; | |
byte data_count = 0, master_count = 0; | |
char key; | |
const int rs = 10, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7; | |
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); | |
/*keymap defines the key pressed according to the row and columns just as appears on the keypad*/ | |
char keymap[numRows][numCols]= | |
{ | |
{'1', '2', '3', 'A'}, | |
{'4', '5', '6', 'B'}, | |
{'7', '8', '9', 'C'}, | |
{'*', '0', '#', 'D'} | |
}; | |
//Code that shows the the keypad connections to the arduino terminals | |
//byte rowPins[numRows] = {2, 3, 4, 5}; //Rows 0 to 3 | |
//byte colPins[numCols]= {6, 7, 8, 9}; //Columns 0 to 3 | |
byte rowPins[numRows] = {A0, A1, A2, A3}; //Rows 0 to 3 | |
byte colPins[numCols]= {A4, A5, 2, 3}; //Columns 0 to 3 | |
//initializes an instance of the Keypad class | |
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); | |
Keypad cKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); | |
void setup() | |
{ | |
myservo.attach(11); | |
//ServoClose(); | |
lcd.begin(16, 2); | |
lcd.print(" Project"); | |
lcd.setCursor(0,1); | |
lcd.print(" Secure Box"); | |
delay(2500); | |
lcd.clear(); | |
lcd.print("****************"); | |
lcd.setCursor(0,1); | |
lcd.print("AVIJIT SENGUPTA"); | |
delay(2500); | |
lcd.clear(); | |
for (int i = 0 ; i < 5 ; i++){ | |
EEPROM.get(i, Master[i]); | |
} | |
lcd.print("Initializing"); | |
lcd.setCursor(12,0); | |
delay(600); | |
lcd.print("."); | |
delay(600); | |
lcd.print("."); | |
delay(600); | |
lcd.print("."); | |
delay(600); | |
lcd.print("."); | |
delay(600); | |
lcd.clear(); | |
lcd.print("Enetr Code:"); | |
lcd.setCursor(0, 1); | |
lcd.print("Pres C to Change Code"); | |
lcd.setCursor(11, 0); | |
Serial.begin(9600); | |
Serial.println("Enter the Password and press # to submit"); | |
Serial.println(Master); | |
myKeypad.addEventListener(keypadEvent); | |
} | |
void(* resetFunc) (void) = 0; //declare reset function @ address 0 | |
void loop() | |
{ | |
key = myKeypad.getKey(); | |
} | |
void keypadEvent(KeypadEvent key){ | |
switch (myKeypad.getState()){ | |
case PRESSED: | |
switch(key){ | |
case 'A': | |
Serial.println("Enter Password"); | |
checkPassword(); | |
break; | |
case 'C': | |
change(); | |
lcd.print(" Code Change Is"); | |
lcd.setCursor(3,1); | |
lcd.print("SUCCESSFUL"); | |
delay(2000); | |
lcd.clear(); | |
lcd.print("RESTARTING..."); | |
delay(2000); | |
resetFunc(); | |
break; | |
case 'D': | |
ServoClose(); | |
lcd.clear(); | |
lcd.print("Box is Locked"); | |
//... code for lock .................. | |
delay(2500); | |
clearData(); | |
lcdHomePage(); | |
break; | |
default: | |
Data[data_count] = key; | |
lcd.print("*"); | |
data_count++; | |
break; | |
} | |
break; | |
} | |
} | |
void lcdHomePage(){ | |
key = {}; | |
lcd.clear(); | |
lcd.print("Enetr Code:"); | |
lcd.setCursor(0, 1); | |
lcd.print("Pres C to Change Code"); | |
lcd.setCursor(11, 0); | |
} | |
void checkPassword(){ | |
Serial.println(Data); | |
if (data_count == Password_Length - 1){ | |
if (!strcmp(Data, Master) || !strcmp(Data, MasterLocal)){ | |
Serial.println("Correct Code"); | |
ServoOpen(); | |
lcd.clear(); | |
lcd.print("Box Unlocked"); | |
delay(3000); | |
lcd.clear(); | |
lcdHomePage(); | |
}else{ | |
Serial.println("Wrong Code"); | |
lcd.clear(); | |
lcd.print("Wrong Password"); | |
lcd.setCursor(0,1); | |
lcd.print("Try Again"); | |
delay(2000); | |
lcdHomePage(); | |
} | |
}else{ | |
Serial.println("Wrong Code"); | |
lcd.clear(); | |
lcd.print("Wrong Password"); | |
lcd.setCursor(0,1); | |
lcd.print("Try Again"); | |
delay(1000); | |
lcdHomePage(); | |
} | |
clearData(); | |
} | |
void change(){ | |
int j=0; | |
lcd.clear(); | |
lcd.print("Enter Existing"); | |
lcd.setCursor(0,1); | |
lcd.print("code: "); | |
lcd.setCursor(6,1); | |
while(j<5) | |
{ | |
char key=myKeypad.getKey(); | |
if(key) | |
{ | |
pass1[j++]=key; | |
//lcd.print(key); | |
} | |
} | |
key={}; | |
delay(500); | |
if(!strcmp(pass1, Master) || !strcmp(pass1, MasterLocal)){ | |
lcd.clear(); | |
lcd.print("Enter New Code:"); | |
lcd.setCursor(0,1); | |
int i = 0; | |
while(i<5) | |
{ | |
char key=myKeypad.getKey(); | |
if(key) | |
{ | |
newPassCode[i++]=key; | |
//lcd.print(key); | |
} | |
} | |
lcd.clear(); | |
lcd.print("Enter Again:"); | |
lcd.setCursor(0,1); | |
int k = 0; | |
while(k<5) | |
{ | |
char key=myKeypad.getKey(); | |
if(key) | |
{ | |
newPassCodeReEntered[k++]=key; | |
//lcd.print(key); | |
} | |
} | |
if (!strcmp(newPassCode, newPassCodeReEntered)){ | |
lcd.clear(); | |
clearEEPROM(); | |
EEPROM.put(0, newPassCode); | |
delay(500); | |
writeCodeToEEPROM(newPassCode,5); | |
}else{ | |
lcd.clear(); | |
lcd.print("Entered Codes"); | |
lcd.setCursor(0,1); | |
lcd.print("are not matching"); | |
delay(2500); | |
lcdHomePage(); | |
} | |
}else{ | |
lcd.clear(); | |
lcd.print("Wrong Code"); | |
lcd.setCursor(0,1); | |
lcd.print("Try Again............"); | |
delay(2500); | |
lcdHomePage(); | |
} | |
} | |
void clearData() | |
{ | |
while (data_count != 0) | |
{ // This can be used for any array size, | |
Data[data_count--] = 0; //clear array for new data | |
} | |
return; | |
} | |
void clearEEPROM(){ | |
for (int i = 0 ; i < EEPROM.length() ; i++) { | |
EEPROM.write(i, 0); | |
} | |
} | |
void writeCodeToEEPROM (char code[5], int len){ | |
//Serial.println(code); | |
char c; | |
//data_count = 0; | |
Serial.println("Storing new code to system variable............."); | |
for (int i = 0 ; i < 5 ; i++){ | |
EEPROM.get(i, Master[i]); | |
//Data[i] = c; | |
} | |
Serial.print("New Value Stored: "); | |
Serial.println(Master); | |
} | |
void ServoOpen() | |
{ | |
for (pos = 90; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees | |
// in steps of 1 degree | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
} | |
void ServoClose() | |
{ | |
for (pos = 0; pos <= 90; pos += 5) { // goes from 180 degrees to 0 degrees | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Secure Box is a arduino based project, whcih provides basic keypad based security locking system. | |
You can unlock the box using your own security key. Key can be changed as well. | |
Below are the basic components required to build this project. | |
a. Arduino Nano (Uno, Pro mini or any other arduino board will work). | |
b. 16x2 LCD display (Parallel display module). | |
c. 4x4 keypad matrix. | |
d. Servo motor | |
e. 7805 IC | |
f. micro usb port. |
Author
avseng
commented
Jan 1, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment