[SOLVED] blinking multiple LEDs independtly without blocking or delay

hi all

i need to be able to blink multiple LEDs independently without delay(),and could switch off or on each LED separately
how can i do this?
i tried many libraries but all can control single LED only or a group of LEDs togther

mestek86:
i tried many libraries but all can control single LED only or a group of LEDs togther

Which library? Can you not instantiate multiple objects from one library, each having its own parameters?

Anyway, have a look at this thread. You should be able to adapt it to your project.

mestek86:
i tried many libraries but all can control single LED

Don't you just need to have as many instances as you need, one per led? (edit: as dougp said :wink: )

Have you tried this adafruit tutorial which is a class "Flasher" for bwod taking in pin, ontime and offtime?

You just have as many Flashers as you need:

Flasher led1(12, 100, 400);  //pin, on, off
Flasher led2(13, 350, 350);

You don't need a library anyway, although sure, it does simplify things.

Have a look at this youtube video which does bwod on two leds.

dougp:
Which library? Can you not instantiate multiple objects from one library, each having its own parameters.

Anyway, have a look at this thread. You should be able to adapt it to your project.

i used this library

and this too

mestek86:
i used this library
...and this too

And did you try multiple instances?

edit, like this (from the 2nd library)

TimedBlink monitor(LED_BUILTIN);
TimedBlink monitorShmonitor(5);

.... and then of course 2 extra lines in setup() and loop() to match?

elvon_blunden:
Don't you just need to have as many instances as you need, one per led? (edit: as dougp said :wink: )

Have you tried this adafruit tutorial which is a class "Flasher" for bwod taking in pin, ontime and offtime?

You just have as many Flashers as you need:

Flasher led1(12, 100, 400);  //pin, on, off

Flasher led2(13, 350, 350);




You don't need a library anyway, although sure, it does simplify things.

Have a look at this [youtube video](https://www.youtube.com/watch?v=3VZgaJBrZD8) which does bwod on two leds.

thank u very much
i will have a look and update u with results

elvon_blunden:
And did you try multiple instances?

no i have not
can this solve the matter?

mestek86:
can this solve the matter?

There's only one way to be sure, and that's to try it...

But both dougp and I were immediately of that opinion, and it certainly works that way for the adafruit Flasher class- their example shows exactly that.

mestek86:
can this solve the matter?

Go for it. If it doesn't work, report your findings and/or error messages. If it does work report that, too. It may help somebody else down the road.

Why use a library ?

unsigned long startTimes[] = {0, 0, 0, 0};
unsigned long periods[] = {1001, 1005, 1007, 1009};    //example periods
const byte ledPins[] = {3, 5, 6, 9};
const int numberOfLeds = sizeof(ledPins) / sizeof(ledPins[0]);

void setup()
{
  Serial.begin(115200);
  Serial.print("Number of LEDs : ");
  Serial.println(numberOfLeds);
  for (int pin = 0; pin < numberOfLeds; pin++)
  {
    pinMode(ledPins[pin], OUTPUT);
  }
}

void loop()
{
  unsigned long currentTime = millis();
  for (int led = 0; led < numberOfLeds; led++)
  {
    if (currentTime - startTimes[led] > periods[led])
    {
      digitalWrite(ledPins[led], !digitalRead(ledPins[led]));
      startTimes[led] = startTimes[led] + periods[led];
    }
  }
}

UKHeliBob:
Why use a library ?

unsigned long startTimes[] = {0, 0, 0, 0};

unsigned long periods[] = {1001, 1005, 1007, 1009};    //example periods
const byte ledPins[] = {3, 5, 6, 9};
const int numberOfLeds = sizeof(ledPins) / sizeof(ledPins[0]);

void setup()
{
  Serial.begin(115200);
  Serial.print("Number of LEDs : ");
  Serial.println(numberOfLeds);
  for (int pin = 0; pin < numberOfLeds; pin++)
  {
    pinMode(ledPins[pin], OUTPUT);
  }
}

void loop()
{
  unsigned long currentTime = millis();
  for (int led = 0; led < numberOfLeds; led++)
  {
    if (currentTime - startTimes[led] > periods[led])
    {
      digitalWrite(ledPins[led], !digitalRead(ledPins[led]));
      startTimes[led] = startTimes[led] + periods[led];
    }
  }
}

and what if i want to switch one of the LEDs can this be made or must be used with all of the array?

elvon_blunden:
And did you try multiple instances?

edit, like this (from the 2nd library)

TimedBlink monitor(LED_BUILTIN);

TimedBlink monitorShmonitor(5);




.... and then of course 2 extra lines in setup() and loop() to match?

yes it worked like a charm

and what if i want to switch one of the LEDs can this be made or must be used with all of the array?

Do you mean that you only want to blink one of the LEDs ?

UKHeliBob:
Do you mean that you only want to blink one of the LEDs ?

under some conditions only one LED blink then switched off when another condition happen and the other turned on blinking and so on

mestek86:
under some conditions only one LED blink then switched off when another condition happen and the other turned on blinking and so on

Add another array that contains a boolean for each LED. In the for loop only change the state of each LED if its corresponding boolean is set to true. Change the value of the boolean when the other event occurs or does not occur, as appropriate.

UKHeliBob:
Add another array that contains a boolean for each LED. In the for loop only change the state of each LED if its corresponding boolean is set to true. Change the value of the boolean when the other event occurs or does not occur, as appropriate.

yes thank u very much