# Write a value to multipe pins with a delay in between

Hi,
I am trying to create a flashing light pattern for the power-on of a project. It calls for 4 LEDs which are side by side to flash in a particular sequence while one in the middle slowly goes from low to full brightness. I have the sequence done, but all 4 LEDs execute the pattern at the same time. I’d like to insert a slight delay between each one so they begin out of sync and appear to sync at the end due to rapid flashing. I don’t know how to do this though. Here’s what I have so far:

``````/*

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.

*/
int brightness = 255;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int fade1 = 20;
int counter1= 0;
int ledpin;
int ledpins[5] = {3, 5, 6, 9, 10};

void setup()  {
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);

}

void loop()  {
// set the brightness of pin 9:
int i;
for (i = 0; i < 5; i = i + 1) {
ledpin = ledpins[i];
}
for (i = 5; i >= 0; i = i - 1) {
ledpin = ledpins[i];
analogWrite(ledpin, brightness);
}

// change the brightness for next time through the loop:
brightness = brightness - fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0) {
brightness = 255 ;
counter1 = (counter1 + 1);
}
if (counter1 == 2 && fade1 > 5) {
counter1 = 0;
Serial.print(ledpin);
}
if (fade1 <= 5 && fade1 > 0 && counter1 == 5) {
counter1 = 0;
}
if (brightness == 0) {
delay(1);
}

if (fade1 == 1) {
brightness = 255;
}

// wait for 30 milliseconds to see the dimming effect
}
``````

It’s a (badly) modified version of one of the example sketches. I’m afraid it lacks proper comments at the moment. If it’s unreadable as-is, I’ll go back and add them. I kind of got carried away getting to where I’m at and neglected to clean it up properly…

Any help would be greatly appreciated!
Thanks,
Static

First, if you only want it for the power on, you should put it in the setup() function. loop() gets called perpetually.

As far as the timing, just inserting delay(ms) statements wherever you want a delay (after each digitalWrite()) would do the trick.

The animation you are describing seems a bit complex, though. If you are comfortable with coding, I say go for it; or if you want the frustration of trial and error, go for it. You will need to visualize the delays and nested for loops to be able to pull it off smoothly - or resort to some kind of scriptable animation system (which could be fun).