Pages: [1]   Go Down
Author Topic: Variable flash rate on Multiple leds without delay  (Read 639 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
*
*  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)
         }
      }
  }
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.

Logged

Pages: [1]   Go Up
Jump to: