I'm trying to figure out how to cycle on and off four LEDs in order (1, 2, 3, 4, 1, 2, 3, etc) regardless of what else is going on in the program (for example: audio playback, button presses, etc).
What would be the best way to make this happen "in the background" of my sketch? I plan on using a WAV shield in this project and would like the LED sequence to continue even when WAV playback is in progress and I've having a hard time wrapping my head around it.
Look into using interrupts. These can run a function at regular intervals.
However very intensive stuff like playing back audio might be disturbed by regularly being interrupted, some things even disable the interrupts so you will have to work on a case by case basis for any project.
Need blink without delay.
Use a long interval to avoid audio hiccups.
Put these bits together:
unsigned long upcomingBlink;
unsigneed long nextBlink;
pinMode(2, OUTPUT);
digitalWrite (2, HIGH);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
pinMode(5, OUTPUT);
digitalWrite(4, LOW);
LEDnumber=2;
void loop()
{
nextblink = millis()-upcomingblink; // covers rollover: current - previous will always be correct
if (nextblink > 2000UL)
{
upcomingblink = millis();
digitalWrite(LEDnumber, LOW);
LEDnumber = LEDnumber +1;
if (LEDnumber = =6){LEDnumber=2;}
digitalWrite(LEDnumber, HIGH);
}
// now do all your other stuff for the next 2 seconds, the stuff, the stuff above will take very little time, especially the majority of the time when the first if is not true ...
}