I've got a state machine that is set up to watch for the press of a button, then turn on/off a group of relays, and after a set amount of time, stop the relays and reset the machine to wait for the button to be pressed again. I'm using Nick Gammon's LED Flasher library (bottom of this page - http://www.gammon.com.au/forum/?id=11411) to update the relays while the timer runs. However, what I've discovered is that if you press the button before the timer has run out, everything starts over again.
What would I do so the code will ignore the button press until the timer is done, and we are back to case 0 (waiting for a button press)?
/*
State Machine with button trigger
Using LedFlasher library to toggle relays
*/
#include <LedFlasher.h>
const int buttonPin = A1; // the number of the pushbutton pin (Analog 1)
const int ReadyLight = 13; // For the green "Ready" light
int buttonState = 0; // variable for reading the pushbutton/breakbeam status
long startTime ; // start time for stop watch
long elapsedTime ; // elapsed time for stop watch
int ShowState = 0; // for controlling the state machine - what is the show doing?
int ShowRun = 8000; // How long show will run. Time in seconds. 1000 = 1 second.
int ResetTime = 8; // Time before show can be triggered again. Time in seconds. 1 = 1 second.
// LED Flasher library - set up
// Configure timing for each output
// If the show is 5000 (5 seconds) then (8, 200, 2500) will toggle the output once
LedFlasher floodLight (8, 200, 1500); // pin 8, off for 200 mS, on for 1500 mS
LedFlasher shuttleBayDoors (9, 300, 600);
LedFlasher impulseEngine (10, 900, 100);
LedFlasher strobe (11, 500, 1000);
LedFlasher navigation (12, 1000, 2000);
LedFlasher torpedoes (7, 250, 500);
LedFlasher Z1 (4, 500, 500);
void setup() {
// LED Flasher commands
floodLight.begin ();
shuttleBayDoors.begin ();
impulseEngine.begin ();
strobe.begin ();
navigation.begin ();
torpedoes.begin ();
Z1.begin ();
// relay module is reversed from LEDs, make sure it's all off by setting to HIGH
// for testing with LEDs, these are flipped to LOW
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(7, HIGH);
digitalWrite(4, HIGH);
pinMode(ReadyLight, OUTPUT); // Set this pin as an output for the "READY" light
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
}
void loop() {
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
if (buttonState == 1)
{
ShowState = 1; // change to case 1 - start the relays
startTime = millis(); // store the start time
}
switch (ShowState) { // switchCase controls the show based on ShowState
case 0: // button not pressed, waiting
digitalWrite(ReadyLight, HIGH); // turn on Ready Light
break;
case 1: // button pressed, start show
digitalWrite(ReadyLight, LOW); // turn off Ready Light
ShowState = 2;
case 2: // show is going, start relays
elapsedTime = millis(); // store elapsed time
if (elapsedTime - startTime >= ShowRun) // while this case loops, check to see if it's time to stop the show
{
ShowState = 3; // stop the show, go to case 3
}
// start relays going. update keeps them blinking while elapsedTime is watched.
floodLight.update ();
shuttleBayDoors.update ();
impulseEngine.update ();
strobe.update ();
navigation.update ();
torpedoes.update ();
Z1.update ();
break;
case 3: // LEDs off
// turn off all relays, since some may be left on when show is stopped
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(7, HIGH);
digitalWrite(4, HIGH);
// Reset time, run quick for...next loop
for(int j = 0; j < ResetTime; j++){
// Serial.print(".");
delay(1000);
}
ShowState = buttonState;
break;
case 4: // Unused
// Serial.println("case 4 - unused");
break;
}
delay(1); // delay in between reads for stability
}