Go Down

Topic: Variable flash rate on Multiple leds without delay (Read 1 time) previous topic - next topic


Hi all,

I'm new to the Arduino and I'm attempting to get a variable number of leds to flash at a fixed on/off rate. The following code almost works, but not quite - What am missing?

Code: [Select]
*  Blink Multiple LEDs without Delay on/off times individually assigned
const int LEDcount = 2; // Number of LEDS used (start @ 2 - KISS)
byte pin[LEDcount] = {9,10}; //Pins assigned to those LEDS
unsigned int oninterval[LEDcount] = {100, 2000}; // On intervial time for leds (max 65535)
unsigned int offinterval[LEDcount] = {2900,2000}; // Off intervial time for leds (max 65535)
unsigned long timeoff[LEDcount] = {0,0}; // What time was the LED last tumned OFF
unsigned long timeon[LEDcount] = {0,0}; // What time was the LED last tuned ON

void setup()
for (int i=0; i<LEDcount; ++i)
   pinMode(pin[i], OUTPUT); //initilize all used pins are outputs

void loop()
unsigned long CurrentTime = millis(); // assign current elapsed time to variable "CurrentTime"
for (int i=0; i<LEDcount; ++i) // loop through all LEDS
         if (CurrentTime - timeoff[i] > offinterval[i])
           digitalWrite(pin[i], HIGH); //Turn LED on
           timeoff[i] = CurrentTime;// Set on timeon = current time (relative 0)

        if (CurrentTime - timeon[i] > oninterval[i])
           digitalWrite(pin[i], LOW); //Turn LED off
           timeon[i] = CurrentTime; // Set on timeon = current time (relative 0)


Please elaborate.  In what way does it "not quite" work?


You don't explicitly record whether each LED is currently ON or OFF. I think your logic would work better if you recorded the state (on/off) and then test whether it is time to change to the other state. Also, since it can only ever be in one state at a time, you only need to hold the start time of the current state.

The basic approach of using global arrays works fine, but there is another approach which would work better on a larger scale and especially if you intend your sketch to deal with things other than LEDs.

Define a class to represent a single flashing LED. Use member variables to hold the configuration and state (pin number, on duration, off duration, state, last change time) and a method to call to update it. Declare an array of them, and in the loop just tell each one to update itself. The update method for each LED would just decide whether it it time to do anything and do whatever was required, then return. If you use a similar non-blocking design for the other things you want your sketch to do, you can combine these little functional units in any way you want and know that they will run independently of each other.


"You don't explicitly record whether each LED is currently ON or OFF."

That has to be the reason it was not working, thanks. I changed the code to add that functionality and will test it tonight. The rest of your suggestion is currently beyond my abilities but does sound the most logical way to proceed once I gain sufficient skills.

Go Up