'delay' with an Array

Brief question:

Can a timing delay be used with an Array such that, say, 3 LEDs are lit in sequence repeatedly with a different delay between them after each pass? For instance: <LED1, delay 100ms, LED2, delay, 100 ms, LED3>, then <LED1, delay 250ms, LED2, delay, 250 ms, #3>, and so on, such that each pass has different (specified) delay between LEDs.

If, so, how might that be coded?


for (int i = 0; i < 10; i++)
  if (previousMillisLed1 + delayArray[i] <= millis())
    //blinking stuff

Could you give me a little more contest on this... please

Did you do the BlinkWithoutDelay example from the Arduino IDE?
You really should take a look at that. There is also a sticky topic in this section called Demonstration code for several things at the same time that further explains the usage of millis() and the principle behind it.

Go through those things and understand them. There really is no practical way of doing what you want without this approach.

Anyway, forget the code above. Now that I look at it again, I see that it’s no good.
You’ll need something like

if (previousMillisLed1 + delayArray1[i] <= millis())
  digitalWrite (led1, led1State);
  led1State = !led1State;
  if (i >= n) //where n is the size of the array holding the delay values
    i = 0;

Once you make and populate an array delayArray1 that holds delays for the first LED and replicate the code for the rest of your LEDs it should work. Make sure you don’t use i in all of them.

  if (previousMillisLed1 + delayArray[i] <= millis())

That’s not what BlinkWithoutDelay or Robin’s thread shows but it could work for almost 50 days with unsigned longs. What you want uses an unsigned subtraction that always returns the difference of two times even across rollovers.

if ( now - start_time >= interval ) // all those being unsigned integers, usually unsigned longs are used
// interval since start_time has passed, do what needs to be done

fprete, are you familiar with the Blink Without Delay example in your IDE?

Shpaget, I can link you to a better, more full yet still beginner-friendly explanation of the subject.

PS extras:

The subject of unsigned math is considered too hard for beginners but it’s at the heart of the time-difference equation and it does work the same right past rollover. Learn how that works and you will never forget how to compute time differences because it won’t be memorization but understanding behind your usage.

The simplest explanation involves how you can tell that 2 o’clock is 3 hours after 11 o’clock. Is that complicated? The clock is unsigned base-12, in computerese the 12 should be 0. Unsigned integers work the same just bigger.

Unsigned math also allows using unsigned 16 and even 8 bit variables to time shorter intervals. I’ve used bytes (unsigned 8 bit) to time button debounces and unsigned ints (16 bit) to time 1 minute or less intervals and they work just fine. If you want to get into it then we can but not in this thread.

Do you need delay after led 1 is turned off and before led 2 is turned on? Same question after led3 off before led1 on again. Do you process anything besides leds, such as button inputs etc.?