Hey everyone,
I put together this code but it's not performing the way I want it to. I'm not sure if it's a hardware or a software issue but I understand that some hardware issues can be overcome with software fixes; maybe this is one of those cases.
Desired outcome: When one of the buttons is pressed ("LeftSwitch" or "RightSwitch"), the corresponding four lights are supposed to run their sequence, then check to see if that button is still pressed. If the button is still pressed, all four lights stay on. If the button is not still pressed, all four lights go off. Within 1/2 second of the button being released, the lights must be ready to run their sequence again. Once a sequence is started, it should run to completion.
Outcome with this code: When a button is pressed, the sequence runs and then stays on or turns off like it should. If the button is released and then pressed again after the sequence runs and turns off, all four lights come on and do not sequence. They will all come on at once and all turn off at once when the button is pressed and released. If some time is allowed to pass with no input (5-10 seconds) and then the button is pressed again, the sequence runs like it should. The sequence does not run to completion if the button is released before the sequence has finished.
Note: I am using the proper pull down resistors to clean up the input. I can post a video as well if the descriptions above aren't useful.
Thanks for looking and thanks for the input!!
unsigned long previousMillisLED6LEFT = 0;
unsigned long previousMillisLED6 = 0;
unsigned long previousMillisX = 0;
unsigned long previousMillisXLEFT = 0;
const int RightSwitch = 40;
const int LeftSwitch = 42;
int intervalLED6 = 300;
int intervalLED5 = 600;
int intervalLED4 = 900;
int intervalLED3 = 1200;
int intervalX = 25;
int intervalLED6LEFT = 300;
int intervalLED5LEFT = 600;
int intervalLED4LEFT = 900;
int intervalLED3LEFT = 1200;
int intervalXLEFT = 25;
// each LED gets a state varaible
boolean LED7state = false; // the LED will turn ON in the first iteration of loop()
boolean LED6state = false; // need to seed the light to be OFF
boolean LED5state = false; // the LED will turn ON in the first iteration of loop()
boolean LED4state = false; // need to seed the light to be OFF
boolean LED3state = false; // the LED will turn ON in the first iteration of loop()
boolean LED2state = false; // need to seed the light to be OFF
boolean LED1state = false; // need to seed the light to be OFF
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
// get current time stamp
// only need one for both if-statements
unsigned long currentMillis = millis();
unsigned long currentMillisLEFT = millis();
/////////////////////////////RIGHT SIDE CODE
if (digitalRead(RightSwitch) == HIGH) {
if ((unsigned long)(currentMillis - previousMillisX) >= intervalLED6) {
digitalWrite(5, HIGH);
previousMillisLED6 = currentMillis;
if ((unsigned long)(currentMillis - previousMillisX) >= intervalLED5) {
digitalWrite(4, HIGH);
if ((unsigned long)(currentMillis - previousMillisX) >= intervalLED4) {
digitalWrite(3, HIGH);
if ((unsigned long)(currentMillis - previousMillisX) >= intervalLED3) {
digitalWrite(2, HIGH);
if ((unsigned long)(currentMillis - previousMillisX) >= intervalX) {
previousMillisX = currentMillis;
}
}
}
}
}
if (digitalRead(RightSwitch) == LOW)
{
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
}
}
//////////////////////////////////////LEFT SIDE CODE
if (digitalRead(LeftSwitch) == HIGH) {
if ((unsigned long)(currentMillisLEFT - previousMillisXLEFT) >= intervalLED6LEFT) {
digitalWrite(9, HIGH);
previousMillisLED6LEFT = currentMillisLEFT;
if ((unsigned long)(currentMillisLEFT - previousMillisXLEFT) >= intervalLED5LEFT) {
digitalWrite(8, HIGH);
// previousMillisLED5 = currentMillis;
if ((unsigned long)(currentMillisLEFT - previousMillisXLEFT) >= intervalLED4LEFT) {
digitalWrite(7, HIGH);
if ((unsigned long)(currentMillisLEFT - previousMillisXLEFT) >= intervalLED3LEFT) {
digitalWrite(6, HIGH);
if ((unsigned long)(currentMillisLEFT - previousMillisXLEFT) >= intervalXLEFT) {
previousMillisXLEFT = currentMillisLEFT;
}
}
}
}
}
if (digitalRead(LeftSwitch) == LOW)
{
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
}
}
}