Created
August 31, 2017 15:17
-
-
Save palucki/fbb63adf62abc1d1822384b2dd2a03e7 to your computer and use it in GitHub Desktop.
Simple arduino-based codelock
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
//Created by Palucki @ April 2014 | |
#include <Servo.h> | |
#define ClosedPos 30 | |
#define OpenedPos 120 | |
Servo MyServo; | |
const int ServoPin = 11; | |
const int RedLedPin = 10 ; | |
const int GreenLedPin = 9 ; | |
const int numRows = 4; // number of rows in the keypad | |
const int numCols = 3; // number of columns | |
const int debounceTime = 50; // number of milliseconds for switch to be stable | |
// keymap defines the character returned when the corresponding key is pressed | |
const char keymap[numRows][numCols] = { | |
{ '1', '2', '3' } , | |
{ '4', '5', '6' } , | |
{ '7', '8', '9' } , | |
{ '*', '0', '#' } | |
}; | |
const int rowPins[numRows] = { 8,7, 6, 5 }; // Rows 0 through 3 | |
const int colPins[numCols] = { 4, 3, 2 }; // Columns 0 through 2 | |
const int pass[4] = {1,2,3,4}; | |
int userpass[4] = {-1, -1, -1 ,-1}; | |
int state; // current state information | |
void setup() | |
{ | |
MyServo.attach(ServoPin); | |
pinMode(GreenLedPin, OUTPUT); | |
pinMode(RedLedPin, OUTPUT); | |
Serial.begin(9600); | |
for (int row = 0; row < numRows; row++) | |
{ | |
pinMode(rowPins[row],INPUT); // Set row pins as input | |
digitalWrite(rowPins[row],HIGH); // turn on Pull-ups | |
} | |
for (int column = 0; column < numCols; column++) | |
{ | |
pinMode(colPins[column],OUTPUT); // Set column pins as outputs | |
// for writing | |
digitalWrite(colPins[column],HIGH); // Make all columns inactive | |
} | |
state = 0; | |
closeLock(); | |
} | |
void loop() | |
{ | |
char key = getKey(); | |
/*States: | |
0 - Idle | |
1 - Insert Code | |
2 - Checking code & Action | |
3 - Closing | |
*/ | |
if(key != -1) // if the character is not -1 then | |
// it's a valid key press | |
{ | |
Serial.println("Got you "); | |
Serial.println(key); | |
if(key == '*') | |
{ | |
key = getKey(); | |
state = 1; | |
} | |
switch(state) | |
{ | |
case 0: | |
Serial.println("I'm idle now"); | |
if (key== '#') | |
closeLock(); | |
break; | |
case 1: | |
getCode(key); | |
break; | |
case 2: | |
if(key == '#') | |
verifyCode(); // check if the code matches the pattern | |
// if user press * we close the lock (state = 3) | |
else | |
updateUserPass(key); | |
break; | |
case 3: | |
closeLock(); | |
break; | |
default: | |
; | |
} | |
} | |
} | |
// returns with the key pressed, or -1 if no key is pressed | |
char getKey() | |
{ | |
char key = -1; // -1 indicates no key pressed | |
for(int column = 0; column < numCols; column++) | |
{ | |
digitalWrite(colPins[column],LOW); // Activate the current column. | |
for(int row = 0; row < numRows; row++) // Scan all rows for | |
// a key press. | |
{ | |
if(digitalRead(rowPins[row]) == LOW) // Is a key pressed? | |
{ | |
delay(debounceTime); // debounce | |
while(digitalRead(rowPins[row]) == LOW) | |
; // wait for key to be released | |
key = keymap[row][column]; // Remember which key | |
// was pressed. | |
} | |
} | |
digitalWrite(colPins[column],HIGH); // De-activate the current column. | |
} | |
return key; // returns the key pressed or -1 if none | |
} | |
void getCode(char uKey) | |
{ | |
//Serial.println("State 1 - Getting Code"); | |
int i=0; | |
while(userpass[i] != -1 && i<3) //sets the position of the cursor | |
i++; | |
userpass[i] = uKey - '0' ; //to get number from char ! | |
if(i == 3) // if we already have 4 digits and type # -> verification | |
// if we have 4 digits and type next, we overwrite the pass | |
state = 2; | |
} | |
void verifyCode() | |
{ | |
bool correct = true; | |
for(int i=0; i<=3; i++) | |
{ | |
if(userpass[i] != pass[i]) | |
{ | |
correct = false; | |
Serial.println("We have an error"); | |
} | |
} | |
if(correct == true) | |
openLock(); | |
else | |
closeLock(); | |
state =0; | |
InitPass(); // re-initializes the user pass | |
} | |
void closeLock() | |
{ | |
MyServo.write(ClosedPos); | |
digitalWrite(GreenLedPin,LOW); | |
digitalWrite(RedLedPin,HIGH); | |
state = 0; | |
} | |
void openLock() | |
{ | |
MyServo.write(OpenedPos); | |
digitalWrite(GreenLedPin,HIGH); | |
digitalWrite(RedLedPin,LOW); | |
} | |
void InitPass() | |
{ | |
for(int i=0; i<3; i++) userpass[i] = -1; | |
} | |
void updateUserPass(char uKey) | |
{ | |
for(int i=0; i<3 ; i++) | |
userpass[i] = userpass[i+1]; | |
userpass[3] = uKey - '0'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment