So the hand in time has passed.
Try to see if you can follow everything in this sketch:
// This demonstrates how to code a simple electronic lock
// using 3 switches wired as below.
// Arduino pin-----SwitchPin1-----SwitchPin2-----GND
// Switch pressed = LOW, released = HIGH
// LarryD
// Version 1.00 16/11/12 code = 1233
// 1.01 17/08/05 added: switch de-bouncing, heartBeatLED stuff
// 1.02 17/08/07 added the following:
// - keyPushedLED, ON when a switch is pushed
// - newCodeLED, ON when ready for new code
// - failedLED, On if the wrong code is entered
//
//We have 3 switches. If the newCodeLED is on, we are allowed 4 switch presses to open the lock.
//Any wrong 4 presses will freeze the program for 10 seconds.
//While frozen any further presses will start the 10 second period over again.
//Four correct presses will turn on the unLockedLED, any further presses will turn off the unLockedLED.
//Each time a switch is pressed the keyPressedLED comes on.
//
//****************
#define OFF HIGH
#define ON LOW
#define Pressed LOW
#define Released HIGH
//****************
const byte keyPushedLED = 5;
const byte failedLED = 6;
const byte newCodeLED = 7;
const byte switch1 = 8;
const byte switch2 = 9;
const byte switch3 = 10;
const byte unLockedLED = 11;
const byte HeartBeatLED = 13;
//****************
byte lastSwitch1 = Released;
byte lastSwitch2 = Released;
byte lastSwitch3 = Released;
byte pushedCounter = 0;
//****************
const unsigned long HeartBeatDelay = 500ul; //500ms
unsigned long HeartBeatMillis;
unsigned long bounceMillis;
const unsigned long bounceDelay = 10ul; //10ms
unsigned long failedMillis;
unsigned long failedDelay = 10 * 1000ul; //10 seconds
//****************
enum MachineStates {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, FAILED, WAIT};
MachineStates STATE = ZERO;
//OR
//enum MachineStates {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, FAILED, WAIT} STATE = ZERO;
// s e t u p ( )
//***********************************************************
void setup()
{
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
pinMode(switch3, INPUT_PULLUP);
pinMode(unLockedLED, OUTPUT);
digitalWrite(unLockedLED, OFF);
pinMode(keyPushedLED, OUTPUT);
digitalWrite( keyPushedLED, OFF);
pinMode(newCodeLED, OUTPUT);
digitalWrite(newCodeLED, OFF);
pinMode(failedLED, OUTPUT);
digitalWrite(failedLED, OFF);
pinMode(HeartBeatLED, OUTPUT);
bounceMillis = millis();
} //END of s e t u p ( )
// l o o p ( )
//***********************************************************
void loop()
{
//****************
//helps check for blocking code
if (millis() - HeartBeatMillis >= HeartBeatDelay)
{
//reset timing
HeartBeatMillis = millis();
//toggle the HeartBeatLED
digitalWrite(HeartBeatLED , !digitalRead(HeartBeatLED));
}
//****************
switch (STATE)
{
case ZERO:
{
//Lock the device
digitalWrite(unLockedLED, OFF);
digitalWrite(newCodeLED, OFF);
pushedCounter = 0;
//Proceed
STATE = ONE;
}
break;
//****************
case ONE:
{
digitalWrite(newCodeLED, ON);
if (readSwitch(switch1, lastSwitch1) == true)
{
pushedCounter++;
//Proceed
STATE = TWO;
}
else if (readSwitch(switch2, lastSwitch2) == true)
{
pushedCounter++;
STATE = FAILED;
}
else if (readSwitch(switch3, lastSwitch3) == true)
{
pushedCounter++;
STATE = FAILED;
}
}
break;
//****************
case TWO:
{
digitalWrite(newCodeLED, OFF);
if (readSwitch(switch2, lastSwitch2) == true)
{
pushedCounter++;
//Proceed
STATE = THREE;
}
else if (readSwitch(switch1, lastSwitch1) == true)
{
pushedCounter++;
STATE = FAILED;
}
else if (readSwitch(switch3, lastSwitch3) == true)
{
pushedCounter++;
STATE = FAILED;
}
}
break;
//****************
case THREE:
{
if (readSwitch(switch3, lastSwitch3) == true)
{
pushedCounter++;
//Proceed
STATE = FOUR;
}
else if (readSwitch(switch1, lastSwitch1) == true)
{
pushedCounter++;
STATE = FAILED;
}
else if (readSwitch(switch2, lastSwitch2) == true)
{
pushedCounter++;
STATE = FAILED;
}
}
break;
//****************
case FOUR:
{
if (readSwitch(switch3, lastSwitch3) == true)
{
pushedCounter++;
//Proceed with unlocking the device
STATE = FIVE;
}
else if (readSwitch(switch1, lastSwitch1) == true)
{
pushedCounter++;
STATE = FAILED;
}
else if (readSwitch(switch2, lastSwitch2) == true)
{
pushedCounter++;
STATE = FAILED;
}
}
break;
//****************
case FIVE:
{
//Unlock the device
digitalWrite(unLockedLED, ON);
//Proceed
STATE = SIX;
}
break;
//****************
case SIX:
{
//Should we lock the device
if (readSwitch(switch1, lastSwitch1) || readSwitch(switch2, lastSwitch2)
|| readSwitch(switch3, lastSwitch3))
{
//Proceed
STATE = ZERO;
}
}
break;
//****************
case FAILED:
{
digitalWrite(newCodeLED, OFF);
if (pushedCounter >= 4)
{
failedMillis = millis();
digitalWrite(failedLED, ON);
//Proceed
STATE = WAIT;
}
//We need 4 presses
if (readSwitch(switch1, lastSwitch1) || readSwitch(switch2, lastSwitch2)
|| readSwitch(switch3, lastSwitch3))
{
pushedCounter++;
}
}
break;
//****************
case WAIT:
{
if (millis() - failedMillis >= failedDelay)
{
digitalWrite(failedLED, OFF);
//Proceed
STATE = ZERO;
}
//NOTE! any switch press will reset the wait period.
if (readSwitch(switch1, lastSwitch1) || readSwitch(switch2, lastSwitch2)
|| readSwitch(switch3, lastSwitch3))
{
//restart timer
failedMillis = millis();
}
}
break;
//****************
} //END of switch/case
} //END of l o o p ( )
//***********************************************************
// r e a d S w i t c h ( )
//***********************************************************
//Return 'true' if the switch was released
bool readSwitch(byte button, byte & lastState)
{
//****************
//Handle switch de-bounce
if (millis() - bounceMillis < bounceDelay)
{
//it is not time yet
return false;
}
//reset timing
bounceMillis = millis();
//****************
byte currentState = digitalRead(button);
if (currentState == lastState)
{
//there was no change
return false;
}
//get eady for the next switch test
lastState = currentState;
//****************
if (currentState == Released)
{
//the button was just released
digitalWrite(keyPushedLED, OFF);
return true;
}
//****************
//the button was just pressed
digitalWrite(keyPushedLED, ON);
return false;
} //END of r e a d S w i t c h ( )
//***********************************************************
// E N D o f S k e t c h
//***********************************************************