Hey guys,
I'm developing a simple door lock keypad and I'm stuck.
The basic idea is that there are 3 buttons linked with the Arduino and you need to press them in the right order to unlock the lock.
I have two problems:
- if the combination is pressed correctly, I want the loop to reset after 6 seconds. (at the moment I need to do the same code again to reset the loop)
- if the combination is pressed incorrectly, I want the loop to reset after 2 seconds, so the person can go again.
HOW CAN I DO THIS????
PLEASE HELP!!!!
My code underneath.
thanks thanks thanks
const int button1 = 4;
const int button2 = 3;
const int button3 = 7;
const int Lock = 6;
const int greenLed = 5;
const int timeConstant = 2500;
int lastEntry = 0;
boolean prevResult;
boolean sameButton;
boolean previResult;
boolean fail;
int greenLedState = 0;
int redLedState = 1;
int buttonPress = 0;
void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(Lock, OUTPUT);
pinMode(greenLed, OUTPUT);
digitalWrite(Lock, HIGH);
}
void loop() {
lastEntry = millis();
buttonPress = 0;
combinationFind(button1, button2, button3);
}//loop
void combinationFind (int p1, int p2, int p3) {
prevResult = true;
buttonPressed(p1, p2);
buttonPressed(p2, p3);
lastButtonPressed(p3);
delay(10);
}//void
void buttonPressed (int b1, int b2) {
if (prevResult == true) {
while (millis() - lastEntry < timeConstant && prevResult == true) {
if (sameButton == true) {
if (digitalRead(b1) == HIGH) {
while (digitalRead(b1) != LOW) {
}//while
prevResult = false;
previResult = false;
delay(10);
lastEntry = millis();
fail = false;
break;
}//if
}//if
else {
if (digitalRead(b1) == HIGH && digitalRead(b2) == LOW) {
while (digitalRead(b1) != LOW) {
}//while
prevResult = false;
previResult = false;
delay(10);
lastEntry = millis();
lastEntry = millis();
fail = false;
break;
}
}//else
}//while
prevResult = false;
previResult = false;
}//if
if (fail == false) {
prevResult = true;
previResult = true;
}
else {
prevResult = false;
previResult = false;
}
}//void
void lastButtonPressed(int b1) {
if (previResult == true) {
while (millis() - lastEntry < timeConstant) {
if (digitalRead(b1) == HIGH) {
delay(50);
lastEntry = millis();
greenLedState = !greenLedState;
redLedState = !redLedState;
digitalWrite(greenLed, greenLedState);
digitalWrite(Lock, redLedState);
fail = false;
delay(2000);
prevResult = true;
previResult = true;
lastEntry = millis();
break;
}
}
}
}