Created
February 11, 2014 17:57
-
-
Save Strikeskids/8940293 to your computer and use it in GitHub Desktop.
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
const int lockpin = 12; | |
const int blinkpin = 13; | |
const int combs[4] = {4,5,6,7}; | |
const int combination[3] = {0,1,2}, combinationLength = 3, inputLength = 100; | |
int input[100], index, prev[4]; | |
boolean locked = true; | |
void setup() { | |
pinMode(lockpin, OUTPUT); | |
pinMode(blinkpin, OUTPUT); | |
for (int i=0;i<4;++i) pinMode(combs[i], INPUT); | |
Serial.begin(56700); | |
} | |
void loop() { | |
int cur[4]; | |
for (int i=0;i<4;++i) { | |
cur[i] = digitalRead(combs[i]); | |
if (cur[i] != prev[i] && cur[i] == 1) { | |
Serial.println(i, DEC); | |
if (i < 3) { | |
if (locked && index < inputLength) { | |
input[index++] = i; | |
} | |
} else { | |
if (locked) { | |
boolean matches = false; | |
if (index == combinationLength) { | |
matches = true; | |
for (int j=0;j<combinationLength;++i) { | |
if (input[j] != combination[j]) matches = false; | |
} | |
} | |
if (matches) { | |
success(); | |
} else { | |
failCode(); | |
} | |
} else { | |
locked = true; | |
} | |
} | |
} | |
} | |
for (int i=0;i<4;++i) { | |
prev[i] = cur[i]; | |
} | |
digitalWrite(lockpin, locked ? HIGH : LOW); | |
delay(15); | |
} | |
void success() { | |
digitalWrite(lockpin, LOW); | |
digitalWrite(blinkpin, HIGH); | |
delay(100); | |
digitalWrite(blinkpin, LOW); | |
locked = false; | |
index = 0; | |
} | |
void failCode() { | |
digitalWrite(blinkpin, HIGH); | |
for (int i=0;i<=4;++i) { | |
delay(200); | |
digitalWrite(blinkpin, i % 2); | |
} | |
locked = true; | |
index = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment