Flash 3 LEDs separately with different start up delays

Hello,

I am a complete noob in the field of Arduino.

Presently I am struggling to create a simple Arduino Program which is as follows:

I want to blink 3 LEDs at 1 Hz rate on separate pins.

The condition is that, only at the start up, meaning when the Arduino is switched ON, the 1st LED should begin flashing normally without any initial delay at 1 Hz rate.

The 2nd LED must start its 1 Hz flashing after about 0.66 seconds behind the 1st LED

The 3rd LED must begin blinking after about 0.66 seconds behind the 3rd LED.

These delays are needed only at the start up, after this the 3 LEDs just go on blinking at there own 1 Hz rate.

I hope somebody will be able to help me out with the code.

Thanks very much in advance.

This seems like a simple enough problem that someone might just go ahead and do it for you. But what do you learn from that? Give it a go yourself and then post the code (using code tags) and we can help you fix what is wrong. People on here are more willing to help someone who shows signs of putting in effort themselves, rather than asking straight up for the answer.
Good luck.

File > Examples > 02.Digital > BlinkWithoutDelay and the associated tutorial:

will provide a useful reference. The only thing it's missing is the startup delay code, but that shouldn't be too difficult to add in to the logic.

Thank you very much for the replies,

If it was a single LED then I could have probably implemented it in the following manner, not sure whether this is correct or not but I assume this may be OK:

void setup(){

delay(x); // x is the required start up delay

pinMode(8, OUTPUT);

}

void loop(){
digitalWrite(8, HIGH);
delayMicroseconds(1000000);
digitalWrite(9, LOW);

}

But since it is for 3 LEDs I am quite confused.

Will it be possible to modify the following code according to my need?

void setup(){
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop(){
digitalWrite(8, HIGH);
delayMicroseconds(1000000);
digitalWrite(9, LOW);
delayMicroseconds(666000);
digitalWrite(9, HIGH);
delayMicroseconds(1000000);
digitalWrite(10, LOW);
delayMicroseconds(666000);
digitalWrite(10, HIGH);
delayMicroseconds(1000000);
digitalWrite(8, LOW);
delayMicroseconds(666000);

}

I want all the pins to oscillate at 1 Hz rate.

At start-up pin#8 should begin its 1 Hz oscillation normally without any delay.

But pin#9 should begin its 1 Hz oscillation after 0.66 seconds behind pin#8,

And pin# 10 should begin its 1 Hz oscillations 0.66 seconds behind pin#9

All the oscillations should begin with a HIGH.

After this the individual pins could simply go on blinking endlessly, until power is switched OFF.

Any help will be highly appreciated!

delay() makes things really easy at first, but it's a real problem if you want to extend a sketch.

Have you seen the blink without delay tutorial? It only flashes one LED but it's the approach you need to take.

I think this will have each LED on for 1 second and off for 1 seconds staggered .666 seconds.

void loop()
{
  digitalWrite(8, HIGH);
  delay(333);
  digitalWrite(10, LOW);
  delay(333);
  digitalWrite(9, HIGH);
  delay(333);
  digitalWrite(8, LOW);
  delay(334);
  digitalWrite(10, HIGH);
  delay(334);
  digitalWrite(9, LOW);

  delay(334);

}

If you did this

void setup(){

delay(x); // x is the required start up delay

pinMode(8, OUTPUT);

}

void loop(){
digitalWrite(8, HIGH);
delayMicroseconds(1000000);
digitalWrite(9, LOW);


}

you would observe the LED as being always on. This is because as soon as it writes LOW to pin 9, it goes back to the start of the loop and writes it high again. The LED would be low for a few nanoseconds.

As others have said, work your way through the "blink without delay" tutorial. It contains very useful concepts. There is a reason several people are recommending it.

There are some more advanced concepts in this snippet including so-called "tenary" operators and doing timing without the dreaded "delay."

You'll learn the importance of this concept as you do more and more complex projects. No time like the present to learn good habits :slight_smile:

//defines the LED connections to your Arduino
//yours may be different...adjust as necessary
const int LED_1 = 9;
const int LED_2 = 10;
const int LED_3 = 11;

//using these defines makes it easy to modify
//for use with LEDs wired to turn on with a HIGH
//at a port pin or a LOW. These are setup for 
//sinking current (LOW = LED on)
#define LED_ON      LOW
#define LED_OFF     HIGH

#define PERIOD      1000    //mS for blink cycles
#define LED_1_DELAY 0       //mS initial delay for LED1
#define LED_2_DELAY 660     //mS initial delay for LED2
#define LED_3_DELAY 1320    //mS initial delay for LED3

//millis() returns unsigned long (a 32-bit number)
//so we need to make our times unsigned longs too.
//these are the timers for the 3 LEDs
//a variable is used for the blink delay
//because we want to be able to configure it
//for the individual start up delays and then 
//for normal 1Hz blinking
unsigned long
    timeNow,
    timer_LED1,
    timer_LED2,
    timer_LED3,
    timer_LED1_Delay,
    timer_LED2_Delay,
    timer_LED3_Delay;

void setup() 
{
    //set up the pins for the LEDs and make
    //sure they start 'OFF'
    pinMode( LED_1, OUTPUT );
    digitalWrite( LED_1, LED_OFF );
    pinMode( LED_2, OUTPUT );
    digitalWrite( LED_2, LED_OFF );
    pinMode( LED_3, OUTPUT );
    digitalWrite( LED_3, LED_OFF );
    
    //initialize the timers
    //grab a snapshot of the current time and set
    //the delays for each LED based on that
    timeNow = millis();
    timer_LED1 = timeNow;
    timer_LED2 = timeNow;
    timer_LED3 = timeNow;
    //set the initial offsets
    timer_LED1_Delay = LED_1_DELAY;
    timer_LED2_Delay = LED_2_DELAY;
    timer_LED3_Delay = LED_3_DELAY;
    
}//setup

void loop() 
{
    //get the current time in mS...
    //use a single read of time for all LEDs so their
    //timing is based off the same frame of reference
    timeNow = millis();

    //and call each LED blink routine
    LEDOne(timeNow);
    LEDTwo(timeNow);
    LEDThree(timeNow);

}//loop

void LEDOne( unsigned long tNow )
{
    //static is used so the variable remains intact
    //when we leave this routine. When we return,
    //"state" will still be here where we left it
    static byte
        state=0x00;

    //check if it's time to change the LED state
    //timer_LED1 was initialized in setup
    //when the timer (tNow) has advanced enough
    //mS, the difference between now (tNow) and then
    //(timer_LED1) will be greater than our delay
    //(timer_LED1_Delay)
    //If not enough time has passed, just leave
    if( (tNow - timer_LED1) < timer_LED1_Delay )
        return;

    //we track the on or off condition of the LED 
    //with the static variable "state". After each
    //delay, we just toggle one bit in this flag
    //so over time it looks like 0-1-0-1-0-1...
    //we use this to know whether to turn the LED
    //on or off (i.e. blink)
    //FYI: the '^' operator is an exclusive-OR 
    state ^= 0x01;

    //the operation sets the output using a "tenary"
    //operator
    //this single line is basically equivalent to:
    //if( state & 0x01 )
    //  digitalWrite( LED_1, LED_ON );
    //else
    //  digitalWrite( LED_1, LED_OFF );
    //
    //This forces the LED_1 output to track the first
    //bit of the state variable (the one we're toggling
    //with the XOR operator above)
    digitalWrite( LED_1, (state & 0x01)?LED_ON:LED_OFF);

    //after each period, set up for the next delay
    timer_LED1 = timeNow;
    //after the intitial delay from setup() we can just
    //use 1000 (1-second) as the delay for blinking
    timer_LED1_Delay = (PERIOD/2);
    
}//LEDOne
    
void LEDTwo( unsigned long tNow )
{
    static byte
        state = 0x00;
        
    if( (tNow - timer_LED2) < timer_LED2_Delay )
        return;

    state ^= 0x01;
    digitalWrite( LED_2, (state & 0x01)?LED_ON:LED_OFF);

    timer_LED2 = timeNow;
    timer_LED2_Delay = (PERIOD/2);
    
}//LEDTwo

void LEDThree( unsigned long tNow )
{
    static byte
        state = 0x00;
        
    if( (tNow - timer_LED3) < timer_LED3_Delay )
        return;

    state ^= 0x01;
    digitalWrite( LED_3, (state & 0x01)?LED_ON:LED_OFF);

    timer_LED3 = timeNow;
    timer_LED3_Delay = (PERIOD/2);
    
}//LEDThree