LEDs working together in different phases

Hi all,

I do not have much experience with the ARDUINO but I have been reading posts for days to understand how to write the following code:

Led 1 is on for 0.3 s and off for 0.7 s (1 second cycle).
0.1 s after the LED1 is on, LED 2 will turn on and stays on for 0.3 s and then turns of for 0.1 s.

From my understanding using 'Delay" doesn't work because there are two LEDs working together with some time delay between them. Using "millis()" would help but I dont know how to apply the 0.1 s delay.

Thank you for your help

1988:
From my understanding using 'Delay" doesn't work because there are two LEDs working together

I think this is not correct. Since there is a sequence to be followed, it is possible to use only the Delay function, as long as it is not necessary to use the microcontroller for other things.

Led 1 is on for 0.3 s and off for 0.7 s (1 second cycle).

LED1 ON

DELAY 300

LED1 OFF

DELAY 700

0.1 s after the LED1 is on, LED 2 will turn on and stays on for 0.3 s and then turns of for 0.1 s.

LED1 ON

DELAY 100ms // 0.1 s after the LED1 is on, LED 2 will turn on

LED2 ON

DELAY 200ms // 300 - 100 = 200

LED1 = 0 // OFF

DELAY 100ms // 200 + 100 = 300 // LED 2 will turn on and stays on for 0.3 s

LED2 OFF

DELAY 100ms // LED 2 will turn on and ... then turns of for 0.1 s

LED2 ON

DELAY 500ms // 700 - 100 - 100 = 500

I just didn't understand how the state of LED2 should be in the next cycle, when LED1 restarts the cycle, because LED2 has been activated and has been activated ever since.

Thank you for your answer,
"LED1 ON
DELAY 100ms // 0.1 s after the LED1 is on, LED 2 will turn on"
This keeps the LED1 on for 0.1s but I want it to be on for 0.3 second and LED2 turns on 0.1s after the first one is on. Led 2 will be on for 0.3 s and off for 0.7s. So they both OK for 0.3s and off for 0.7 s with 0.1s delay between the two o turn on.

Thank you

The cycle is reapiting, so I want this to happen over and over. Do you know how I can make that happen?

instead of using delay() you can use millis() to capture a timestamp and compare it to a previous timestamp to determine when to do something without interfering other activities.

#define LedPin          13
#define Led1OnMsec      700
#define Led1OffMsec     (1000 - Led1OnMsec)
void
leds () {
    static unsigned long msecLst = 0;
           unsigned long msec = millis();
    static bool          led1State = LOW;

    if (led1State && (msec - msecLst) > Led1OnMsec)  {
        led1State = LOW;
        digitalWrite (LedPin, led1State);
        msecLst =msec;
    }
    else if (! led1State && (msec - msecLst) > Led1OffMsec)  {
        led1State = HIGH;
        digitalWrite (LedPin, led1State);
        msecLst =msec;
    }
}

Thank you for your answer,
How I can add the LED2 which starts 0.1s after LED 1 is on?

Thank you

In the following code, is there any way I can apply a time delay between two LEDs?

// These variables store the flash pattern
// and the current state of the LED
     
int ledPin1 =  12;      // the number of the LED pin
int ledState1 = LOW;             // ledState used to set the LED
unsigned long previousMillis1 = 0;        // will store last time LED was updated
long OnTime1 = 250;           // milliseconds of on-time
long OffTime1 = 750;          // milliseconds of off-time
     
int ledPin2 =  13;      // the number of the LED pin
int ledState2 = LOW;             // ledState used to set the LED
unsigned long previousMillis2 = 0;        // will store last time LED was updated
long OnTime2 = 330;           // milliseconds of on-time
long OffTime2 = 400;          // milliseconds of off-time
     
void setup() 
{
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT);      
  pinMode(ledPin2, OUTPUT);      
}
     
void loop()
{
  // check to see if it's time to change the state of the LED
  unsigned long currentMillis = millis();
     
  if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
  {
    ledState1 = LOW;  // Turn it off
    previousMillis1 = currentMillis;  // Remember the time
    digitalWrite(ledPin1, ledState1);  // Update the actual LED
  }
  else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
  {
    ledState1 = HIGH;  // turn it on
    previousMillis1 = currentMillis;   // Remember the time
    digitalWrite(ledPin1, ledState1);	  // Update the actual LED
  }
      
  if((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
  {
    ledState2 = LOW;  // Turn it off
    previousMillis2 = currentMillis;  // Remember the time
    digitalWrite(ledPin2, ledState2);  // Update the actual LED
  }
  else if ((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
  {
    ledState2 = HIGH;  // turn it on
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(ledPin2, ledState2);	  // Update the actual LED
  }
}

It appears that you want LED changes to be possible on .1s boundaries
and that these changes should be repeatable every second.

Construct code using millis( ) that 1) detects an elapsed time of .1s
from the previous detection, 2) counts each detection 1 thru 10 (Tenth),
and 3) sets a flag (Frame) indicating this detection.

Your LED code (a section for each LED) looks for Frame to be true. If not,
it does nothing. If so, it tests Tenth to see what action to take. At
the end of all the LED code, remember to clear Frame for next time.

Now you can set any LED to go on or go off by looking at Tenth. The LED
code will have a variable 1 - 10 for ON, and another variable 1- 10 for OFF.
No delays anywhere and millis( ) is only in one place.

Thank you,

Can you please show me how to code this, I am very new to Arduino coding.

I would start with writing the timing loop and just toggling one LED on one of the Tenth values (say 10).
Look for millis tutorials, and how to drive an LED.
Once we see your attempt at that, we can go from there.

LED2 turns ON at 0.1 sec, for 0.3 sec, time now = 0.4 sec, stays OFF for 0.1 sec, time now 0.5 sec, what now?

Like this?

void loop()
{
   digitalWrite(LED1,HIGH);
   delay(100);
   digitalWrite(LED2,HIGH);
   delay(200);
   digitalWrite(LED1,LOW);
   delay(100);
   digitalWrite(LED2,LOW);
   delay(600);
}

EDIT: OOPs, lower case "d" in digitalWrite.
Or like this?

byte LED1 = ?, LED2 = ?; // Your LED pin numbers

void setup()
{
  //Serial.begin(9600);
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
}
void loop()
{
   static unsigned long tStart;
   unsigned long ET = millis() - tStart;
   int tEnd = 1000;
   digitalWrite(LED1,ET <= 300);
   digitalWrite(LED2,ET >= 100 && ET <= 400);
   if(ET > tEnd)
     tStart += tEnd;
}

JCA34F:
LED2 turns ON at 0.1 sec, for 0.3 sec, time now = 0.4 sec, stays OFF for 0.1 sec, time now 0.5 sec, what now?

Are you the OP?

I was thinking more like:

unsigned long start_t = 0
unsigned long CENTA   = 100
         byte TENth   = 0 
         bool Frame   = False


void setup( ) {
    pinMode( <led_pin_1>, OUTPUT )
    pinMode( <led_pin_2>, OUTPUT )
}


void loop( ) {
    if( (millis( ) - start_t) > CENTA ) {
        start_t = millis( );
        Frame = True;
        if( ++TENth > 10 )
            TENth = 1
    }

    if( Frame ) {
        const byte led1_timeon  = 2;    // edit values to change pattern
        const byte led1_timeoff = 5;
        const byte led2_timeon  = 7;
        const byte led2_timeoff = 9;

        if( TENth == led1_timeon )  digitalWrite( <led_pin_1>, LOW );
        if( TENth == led1_timeoff ) digitalWrite( <led_pin_1>, HIGH );
        if( TENth == led2_timeon )  digitalWrite( <led_pin_2>, LOW );
        if( TENth == led2_timeoff ) digitalWrite( <led_pin_2>, HIGH );

        Freme = False;
    }
}