# Fading LED Array with millis()

Hi All,

I am trying to adapt the Array Tutorial sketch to create an LED chaser effect. As the sketch uses Delay() I am trying to convert it to millis() for the first step.
Once I have the whole Thing running on Millis() I would like to make it so that the LED Array not just lit as High or Low, but something like:

LED-9 (Low)
LED-8 (Low)
LED-7 (High)
LED-6 (66% brightness)
LED-5 (33% brightnes)
LED-4 (Low)
LED-3 (Low)
LED-2 (Low)
LED-1 (Low)

Kind of like a Fading tail behind the lit LED.

That second step is just so you know where I want to go with this. for the Moment, I will be completely happy if I can get rid of the delay.

Here is the code that I have so far which works but with Delay. I have commented out all my Trials and Errors as they did not work.

`````` /*
Arrays

Unlike the For Loop tutorial, where the pins have to be
contiguous, here the pins can be in any random order.

The circuit:
* LEDs from pins 2 through 7 to ground

created 2006
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Array
*/

int timer = 250;           // The higher the number, the slower the timing.
int ledPins[] = {
9, 10, 11, 12, 13 };       // an array of pin numbers to which LEDs are attached
int pinCount = 5;           // the number of pins (i.e. the length of the array)
int midPin;
int lastPin;
int offPin;

unsigned long TIMER_Chaser = 0;
unsigned long INTERVAL_Chaser;

void setup() {
TIMER_Chaser = millis();

// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
pinMode(ledPins[thisPin], OUTPUT);
}

}

void loop() {
// delay with millis()
//if(millis() - TIMER_Chaser > INTERVAL_Chaser){

//loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) { //lastPin = thisPin -1 etc.
/*  midPin = thisPin--;
lastPin = midPin--;
offPin = lastPin--;
*/
digitalWrite(ledPins[thisPin], HIGH);     // turn the pin on
/*     analogWrite(ledPins[midPin], 168);     // dim the pin after by 33%
analogWrite(ledPins[lastPin], 84);     // dim the 2nd pin after by 66%
analogWrite(ledPins[offPin], LOW);       // turn the pin off

TIMER_Chaser = millis();    */
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}

}

//  }
``````

The key thing to remember here is that in loop(), you cannot use a "for" loop like that.
Essentially, a for loop says "I want to repeat the following events one after the other, as quickly as possible".
But the "as quickly as possible" is exactly what you don't want (and leads inevitably to the use of delay() ), so, you need to handle the variable sequencing explicitly each time you have a timing event.

For how to make your own timing event, see the "blink without delay" example in the IDE, or Robin2's tutorial on how to do several things at once.

AWOL:
[...] is exactly what you don't want (and leads inevitably to the use of delay() ), so, you need to handle the variable sequencing explicitly each time you have a timing event.

Hi AWOL,
Thank you for the reply. Yes, that makes sense. Looks like it is back to the drawing board.