Skip to content

Instantly share code, notes, and snippets.

@jhynes94
Created September 19, 2014 16:45
Show Gist options
  • Save jhynes94/457febef8dad19b5a6a0 to your computer and use it in GitHub Desktop.
Save jhynes94/457febef8dad19b5a6a0 to your computer and use it in GitHub Desktop.
// myservo.write(0); //Clockwise Unlocks Door
// myservo.write(360); //CounterClockwise Locks Door
// myservo.detach(10); //Turns Servo OFF
// myservo.attach(10); //Enables servo on pin 9
#include <Servo.h>
#include <Keypad.h>
//Servo Setup
Servo myservo; // create servo object to control a servo
//----------------------KeyPad Setup--------------------------------
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'0','F','E', '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 );
//----Password Setup-----
char Password[5] = {'1', '2', '3', '4'};
char PassGuess[5] = {'0', '0', '0', '0'};
void setup()
{
Serial.begin(9600);
Serial.println("The Following Program acts as a door lock with a 4x4 Keypad");
Serial.print("Current Password: ");
Serial.println(Password);
}
void loop()
{
char key = keypad.getKey();
if (key){
Serial.println("Password Attempt:");
Serial.println(key);
//Password Que that passes values down line
PassGuess[0] = PassGuess[1];
PassGuess[1] = PassGuess[2];
PassGuess[2] = PassGuess[3];
PassGuess[3] = key;
Serial.println(PassGuess);
}
//Checks to see if Password is correct
if ((PassGuess[0] == Password[0]) && (PassGuess[1] == Password[1]) && (PassGuess[2] == Password[2]) && (PassGuess[3] == Password[3])){
Serial.println("Door Unlocked");
UnlockDoor();
//Resets Password
PassGuess[0] = '0';
PassGuess[1] = '0';
PassGuess[2] = '0';
PassGuess[3] = '0';
}
if (key == 'D'){
Serial.println("Door Locked");
LockDoor();
}
}
void LockDoor(){
myservo.attach(10);
myservo.write(180); //Clockwise Unlock
delay(530);
myservo.detach();
}
void UnlockDoor(){
myservo.attach(10);
myservo.write(0); //Clockwise Unlock
delay(750);
myservo.detach();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment