Running two line at differnet delay

HI guys, I'm new at arduino, so i want to make 2 LED to On and off but different delay
LED 1

digitalWrite(LED_1,HIGH);
delay(2000);
digitalWrite(LED_1,HIGH);
delay(500);

and
LED 2

digitalWrite(LED_2,HIGH);
delay(500);
digitalWrite(LED_2,HIGH);
delay(2000);

how to combine these 2 different line code in one program??

ok, start with looking at the blink without delay example in the IDE.

You have fallen into the standard beginners trap in Arduino of using 'delay'. This is sometimes used in tutorials because it is simple but it is not useful for any significant code. Delay means to stop for a period of time, it is blocking code. The microcontroller does nothing and therefore can not switch any other LED or check buttons etc.

You need to use millis timing to control your LEDs. That is explained in the example above. With millis timing you need to grasp the concept of loop. Your code in the loop function will repeat over and over again very fast. The faster the better. Every loop the microcontroller can check the time and see if you need to turn on or off any led. It allows you to set any 'delayTime' you like but you cant use 'delay'.

1 Like

Example-code for timing based on millis()

BlinkWithoutDelay

Arduino Multiple Things

Several Things at a Time

const byte
LED1 = 3,
LED2 = 7
;

boolean LEDstate = false;

void setup ()
{
  pinMode ( LED1, OUTPUT );
  pinMode ( LED2, OUTPUT );
}

void loop ()
{
  LEDstate = ( millis () / 500 ) % 5;
  digitalWrite ( LED1, LEDstate );
  digitalWrite ( LED2, !LEDstate );
}

Since all you ever do is turn on both LEDs, this modification of @flashko's code should work:

const byte
LED1 = 3,
LED2 = 7
;

void setup ()
{
  pinMode ( LED1, OUTPUT );
  pinMode ( LED2, OUTPUT );

  digitalWrite ( LED1, HIGH );
  digitalWrite ( LED2, HIGH );
}

void loop ()
{
}

But even if we cut you the slack you need, and take what you meant to say, you probably would have been better off not leaving a wide open path by asking for

digitalWrite(LED_1,HIGH);
delay(1900);
digitalWrite(LED_1,LOW);
delay(377);

and

digitalWrite(LED_2,HIGH);
delay(1700);
digitalWrite(LED_2,LOW);
delay(277);

for which the answers given by @pmagowan and @ToddL1962 are appropriate.

a7

void loop() {
  unsigned long time = millis();
  digitalWrite(LED_1,(time%2500)<2000);
  digitalWrite(LED_2,(time%2500)<500);
}

So how does this work?
The variable time % 2500 returns a value between 0 and 2500 or the remainder when divided by 2500

With only the remainder from the division remaining we check to see if it is < Value and if true turn the light on.

Z

Did you mean to turn the LEDs ON and OFF:

// LED_1: On for 2000, off for 500
digitalWrite(LED_1,HIGH);
delay(2000);
digitalWrite(LED_1,LOW);
delay(500);

// LED_2: On for 500, off for 2000
digitalWrite(LED_2,HIGH);
delay(500);
digitalWrite(LED_2,LOW);
delay(2000);

You can combine those two patterns with:

digitalWrite(LED_1,HIGH);
digitalWrite(LED_2,HIGH);
delay(500);
digitalWrite(LED_2,LOW);  // LED_2 was on for 500
delay(1500);
digitalWrite(LED_1,LOW); // LED_1 was on for 2000
delay(500);
// LED_1 has been off for 500, LED_2 has been off for 2000

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.