Help with my key lock

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:

  1. 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)
  2. 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;
}
}
}
}

How are your buttons wired? You are declaring them as INPUT so they require an external pull-up resistor.

You should also look at the BlinkWithoutDelay example as well as this thread for how to do multiple things at the same time.

Read "How To Use This Forum"

in particular, 7. If you are posting code or error messages, use "code" tags

This is what happens when you do not

A different back-of-a-napkin approach -

Two arrays. One holds the combination the other the entered key numbers.

Entry number counter.

When first button is pressed counter is at zero. Store button code in buttonArray at an index given by counter. Increment the counter. Next button press is stored in buttonArray as above. Third button press as before.

When key is released on third button press compare the two arrays. If they're equal at each location a correct combination has been entered. If not, well...

Follow up on blh64's advice. Also familiarize yourself with IDE -> file/examples/digital/stateChangeDetection and switch debouncing.

Great!
Thanks guys.
I'll have a look.