# LED double flashing 

**URL:** <https://forum.arduino.cc/t/led-double-flashing/1061511>\
**Category:** LEDs and Multiplexing\
**Created:** [December 4, 2022, 9:54pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511 "2022-12-04T21:54:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![niicoo001](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@niicoo001](https://forum.arduino.cc/u/niicoo001)\
**Post date:** [December 4, 2022, 9:54pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/1 "2022-12-04T21:54:13Z")

</div>

Hello,

I want to use my Arduino Nano to make several LEDs blink twice each with a short pause in between. So it should be a double flash.  
I have managed this without problems with delay().  
But since I want to flash the LED in different clocks I have to switch to millis as far as I know.

I have now tried this and I manage to make one LED blink, but I do not know how to make the LED blink twice and then insert a pause.

Can someone help me?

---

<div class="post-metadata">

**Author:** ![red\_car](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@red\_car](https://forum.arduino.cc/u/red_car)\
**Post date:** [December 4, 2022, 9:57pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/2 "2022-12-04T21:57:24Z")

</div>

> [@niicoo001](#):
>
> I have now tried this and I manage to make one LED blink, but I do not know how to make the LED blink twice and then insert a pause.

Show us your code that you attempted.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [December 5, 2022, 12:04am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/3 "2022-12-05T00:04:20Z")

</div>

Start with the "blink without delay" code, but alternate between a short and long interval.

---

<div class="post-metadata">

**Author:** ![niicoo001](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@niicoo001](https://forum.arduino.cc/u/niicoo001)\
**Post date:** [December 5, 2022, 7:18am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/5 "2022-12-05T07:18:51Z")

</div>

This is my code for a LED to flash permanently. But now I have no idea how to proceed.  
I want the LED to flash twice quickly and then have a pause of x milliseconds and then flash twice quickly again and so on.

```cpp
int ledVL = 12;

int ledVL_State = LOW;

unsigned long previousMillis = 0;

const long intervalON = 100;
const long intervalOFF = 600;

void setup() {
  // put your setup code here, to run once:
    pinMode(ledVL, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= intervalOFF) {
    previousMillis = currentMillis;
    ledVL_State = HIGH;
    
  } else if (currentMillis - previousMillis >= intervalON) {
    ledVL_State = LOW;
  }

  digitalWrite(ledVL, ledVL_State);

}

```

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [December 5, 2022, 8:07am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/6 "2022-12-05T08:07:21Z")

</div>

> [@niicoo001](#):
>
> since I want to flash the LED in different clocks I have to switch to millis

It's not clear what you mean by "in different clocks", but it does not sound like a reason to use millis().

Try to do it with millis() _delay()_ and let us know if that works for you or not.

---

<div class="post-metadata">

**Author:** ![niicoo001](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@niicoo001](https://forum.arduino.cc/u/niicoo001)\
**Post date:** [December 5, 2022, 8:30am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/7 "2022-12-05T08:30:07Z")

</div>

All LEDs should flash twice at the same speed but all should start at a different time and also overlap.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [December 5, 2022, 8:41am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/8 "2022-12-05T08:41:14Z")

</div>

I think this could still be done with millis() _delay()_, depending on the details. Can you give some examples of different start times and flash speeds?

---

<div class="post-metadata">

**Author:** ![niicoo001](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@niicoo001](https://forum.arduino.cc/u/niicoo001)\
**Post date:** [December 5, 2022, 9:14am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/9 "2022-12-05T09:14:29Z")

</div>

I would like to equip model of an ambulance with signal light - something like this: [Ambulance Model](https://www.youtube.com/watch?v=Tht6ECYszeg)  
Only I want the rear two lights to flash 2 times at the same time.  
But all in all it should look like in the video in terms of speed and timing.

---

<div class="post-metadata">

**Author:** ![misibacsi](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@misibacsi](https://forum.arduino.cc/u/misibacsi)\
**Post date:** [December 5, 2022, 10:34am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/10 "2022-12-05T10:34:38Z")

</div>

I have a similar software, with shorter and longer flashes and pauses between them. It uses the builtin LED.

```cpp
const int ledPin = LED_BUILTIN; // the number of the LED pin

float ti = 200; // shor point time (.) 
float ta = 3 * ti; // longer dash time (-) 3 * point

  int r() // short LED flash, ti
    {
    imp_startTime = millis(); // start of impulse
    while (millis() - imp_startTime < ti)
        {
          digitalWrite(ledPin, HIGH);
        }
      digitalWrite(ledPin, LOW);
                            
    imp_startTime = millis(); // start of impulse
    while (millis() - imp_startTime < ti)
        {
        }
    }

  int h() // longer LED flash, tá
    {
    imp_startTime = millis(); // start of impulse
    while (millis() - imp_startTime < ta)
        {
          digitalWrite(ledPin, HIGH);
        }
      digitalWrite(ledPin, LOW);

    imp_startTime = millis(); // start of impulse
    while (millis() - imp_startTime < ti)
        {
        }
    }

  int sz() // pause between flashes = tá
    {
    while (millis() - imp_startTime < ta)
        {
        }

// LED flashes with different length
// tizesA = desired number of flashes

void LEDVillant()
  {   
  unsigned long startTime_LEDVillant;
  startTime_LEDVillant = millis(); // start of impulse

  Serial.print("LED: ");

  Serial.print(tizesA, 1);
  switch (tizesA) 
    {

    case 0:
// Serial.print("0 ");
      h();
      h();
      sz();
      break;

    case 1:
// Serial.print("1 ");
      r();
      sz();
      break;

    case 2:
// Serial.print("2 ");
      r();
      r();
      sz();
      break;

    case 3:
// Serial.print("3 ");
      r();
      r();
      r();
      sz();
      break;

    case 4:
// Serial.print("4 ");
      r();
      r();
      r();
      r();
      sz();
      break;

    default:
      // code to be executed if
      // expression doesn't match any constant
      break;

```

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [December 5, 2022, 10:41am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/11 "2022-12-05T10:41:07Z")

</div>

Your code in the post5 already used different timing for ON and OFF intervals. All what you need - add a one more modification - set a two different timings for short and long OFF intervals between the flashing.

To use the intervals in right oreder we need a state machine. In simplest form it can be a state variable and switch/case operator:

```cpp
void loop() {

  unsigned long currentMillis = millis();
  unsigned long intervalON = 100;
  unsigned long intervalOFFshort = 100;
  unsigned long intervalOFFlong = 500;
  static unsigned long currentInterval = intervalON;
  enum states {First_ON, Short_OFF, Second_ON, Long_OFF, Last_State};
  static int state = First_ON;
  if (currentMillis - previousMillis >= currentInterval) {
    previousMillis = currentMillis;

    switch (state) {
      case First_ON:
      case Second_ON:
        ledVL_State = HIGH;
        currentInterval = intervalON;
        break;
      case Short_OFF:
        ledVL_State = LOW;
        currentInterval = intervalOFFshort;
        break;
      case Long_OFF:
        ledVL_State = LOW;
        currentInterval = intervalOFFlong;
        break;

    }
    state++;
    if (state == Last_State) state = First_ON;
    digitalWrite(ledVL, ledVL_State);
  }

}

```

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [December 5, 2022, 10:47am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/12 "2022-12-05T10:47:23Z")

</div>

Your while loop is perfectly equivalent to the delay() operator:

> [@misibacsi](#):
>
> ```arduino
> while (millis() - imp_startTime < ti)
> {
> digitalWrite(ledPin, HIGH);
> }
> 
> ```

This is a good example how SHOULDN'T use millis().

---

<div class="post-metadata">

**Author:** ![niicoo001](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@niicoo001](https://forum.arduino.cc/u/niicoo001)\
**Post date:** [December 5, 2022, 11:04am UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/13 "2022-12-05T11:04:36Z")

</div>

Thank you very much! That is exactly how I wanted it.  
I don't know why I didn't think of this way.

Could you give me another tip how I do it now with multiple LEDs? So that two LEDs flash at the same time and others in a different interval?

---

<div class="post-metadata">

**Author:** ![misibacsi](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@misibacsi](https://forum.arduino.cc/u/misibacsi)\
**Post date:** [December 5, 2022, 12:55pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/14 "2022-12-05T12:55:09Z")

</div>

Do you also have an explanation, why?

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [December 5, 2022, 12:57pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/15 "2022-12-05T12:57:06Z")

</div>

> [@niicoo001](#):
>
> all should start at a different time and also overlap.

I don't see any overlap in the video. This looks very simple and can be done with delay(). I don't see the need for state machines or use of millis().

EDIT: I now realise I said confusing things in posts #6 and #8. I meant to suggest delay() not millis(). I have corrected those posts. Sorry for confusion!

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [December 5, 2022, 12:58pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/16 "2022-12-05T12:58:32Z")

</div>

The idea of using millis() instead of delay() is to be able to set aside a thing for a time and when that thing is set aside for a while to be able to do another thing.

> [@misibacsi](#):
>
> `while (millis() - imp_startTime < ti) { digitalWrite(ledPin, HIGH); }`

When this thing, the above code, is run the other things cannot run. Defeating a reason why to use millis().

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [December 5, 2022, 12:58pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/17 "2022-12-05T12:58:44Z")

</div>

> [@misibacsi](#):
>
> Do you also have an explanation, why?

Explanation of what?

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [December 5, 2022, 1:05pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/18 "2022-12-05T13:05:42Z")

</div>

> [@niicoo001](#):
>
> Could you give me another tip how I do it now with multiple LEDs?

In most general case you need as much state machines as many leds you have. But if your leds has something similar - the same interval, for example - you can combine several leds in a one state machine.

---

<div class="post-metadata">

**Author:** ![misibacsi](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@misibacsi](https://forum.arduino.cc/u/misibacsi)\
**Post date:** [December 5, 2022, 1:11pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/19 "2022-12-05T13:11:47Z")

</div>

Why using millis() is not good in my code?  
I hope to learn from your answer.

Previously I already tried to use delay(), but it regularly stopped the code.  
Other parts of the code do some computing, and need to run continously.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [December 5, 2022, 1:19pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/20 "2022-12-05T13:19:20Z")

</div>

> [@misibacsi](#):
>
> Why using millis() is not good in my code?

it is not "wrong", just useless.  
The while loop is blocking equally as delay(), so the using millis() with while does not able the code run several tasks in the time.  
Please read the BlinkWithoutDelay example to see the way how to use millis correctly

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [December 5, 2022, 1:25pm UTC](https://forum.arduino.cc/t/led-double-flashing/1061511/21 "2022-12-05T13:25:39Z")

</div>

> [@misibacsi](#):
>
> Why using millis() is not good in my code?  
> I hope to learn from your answer...  
> Other parts of the code do some computing, and need to run continously.

@misibacsi you are now asking for help with your own code and understanding and so you are hijacking the topic begun by @niicoo001. Please start your own topic to continue that discussion. You can notify @b707 or myself or others to that topic using "@".

@niicoo001: unlike @misibacsi, your code does not need to perform other background computing tasks, so in my view you can use delay() to time the flashing, which is much simpler.

[Next page](https://forum.arduino.cc/t/led-double-flashing/1061511.md?page=2)
