I am trying to use a pushbutton to toggle between a series of different blinking LED patterns. My abbreviated code is below.
The issue I have is that my various patterns take several seconds to execute and I only perform a digitalRead once per loop (at the beginning). Therefore, if I am in the middle of a light pattern and press the pushbutton to activate a new pattern, my input is not picked up. I have to press and hold the button while the first LED pattern finishes before I can register that the pushbutton is being pressed.
Is there a better way to structure the code so that I can toggle between light patterns, even if I am in the middle of executing a given pattern?
I'm still new to Arduino and programming so if there is a concept or tutorial you can point me to, I'd appreciate it.
Thanks in advance.
Samir
int switchState1 = 0;
int mode = 0;
void setup() {
// put your setup code here, to run once:
}
void LEDPattern0() {
...
}
void LEDPattern1() {
...
}
void LEDPattern2() {
...
}
void loop() {
// put your main code here, to run repeatedly:
switchState1 = digitalRead(2);
if ( switchState1 == HIGH ) {
if ( mode == 0 ) {
mode = 1;
} else if ( mode == 1 ) {
mode = 2;
} else if ( mode == 2 ) {
mode = 0;
}
}
if (mode == 0) {
LEDPattern0();
}
if (mode == 1) {
LEDPattern1();
}
if (mode == 2) {
LEDPattern2();
}
delay(2000);
} // end loop()