Last active
December 6, 2015 20:41
-
-
Save andrewvaughan/adbe93c705f02cce5f2b to your computer and use it in GitHub Desktop.
This file contains 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 <stdbool.h> | |
#include <AnalogKeypad.h> | |
#include <Servo.h> | |
// Arduino Pin Assignments | |
#define PIN_KEYS A0 | |
#define PIN_SERVO 9 | |
#define PIN_LED 13 | |
// Servo Configurations | |
#define SRV_DELAY 5000 | |
#define SRV_PASSIVE 10 // 10 degree angle passive (0 fails) | |
#define SRV_ACTIVE 20 // 20 degree angle active | |
AnalogKeypad keyPad = AnalogKeypad(PIN_KEYS); | |
Servo srvLatch; | |
int valid[] = { | |
(int)KEY_3, | |
(int)KEY_2, | |
(int)KEY_5, | |
(int)KEY_2, | |
(int)KEY_0, | |
(int)KEY_1, | |
(int)KEY_6 | |
}; | |
int pointer = 0; | |
bool keyReset = true; | |
// Determine the length of the password based on byte division | |
int passLength = (sizeof(valid) / sizeof(int)) - 1; | |
/** | |
* Setup function called prior to main loop. | |
*/ | |
void setup() { | |
pinMode(PIN_LED, OUTPUT); | |
Serial.begin(115200); | |
// Initialize the keypad module | |
keyPad.init(); | |
// Reset our Servo to the locked state | |
srvLatch.attach(PIN_SERVO); | |
srvLatch.write(SRV_PASSIVE); | |
} | |
/** | |
* Main loop function. | |
*/ | |
void loop() { | |
int input = keyPad.readKey(); | |
// Skip out early if we're not pressing anything | |
if (input == KEY_NONE) { | |
keyReset = true; | |
return; | |
} | |
// Parse a keypress if we've had a reset | |
if (keyReset) { | |
// Blink the LED | |
digitalWrite(PIN_LED, HIGH); | |
delay(250); | |
digitalWrite(PIN_LED, LOW); | |
// Check if this entry of the passcode is correct | |
if (valid[pointer] == (int)key) { | |
pointer++; | |
Serial.print("Key "); | |
Serial.print(pointer); | |
Serial.print(" of "); | |
Serial.print(passLength); | |
Serial.print(" == "); | |
Serial.println(key); | |
// Valid password entered, open the latch temporarily | |
if (pointer >= passLength) { | |
Serial.println("***VALID***"); | |
srvLatch.write(SRV_ACTIVE); | |
delay(SRV_DELAY); | |
srvLatch.write(SRV_PASSIVE); | |
pointer = 0; | |
} | |
} else { | |
Serial.println("Invalid key, resetting."); | |
pointer = 0; | |
} | |
// Make sure there is a KEY_NONE before we process again | |
keyReset = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment