LED double flashing

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?

Show us your code that you attempted.

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

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.

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);

}

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.

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

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?

I would like to equip model of an ambulance with signal light - something like this: Ambulance Model
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.

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

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;


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:

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);
  }

}

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

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

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?

Do you also have an explanation, why?

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!

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.

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

1 Like

Explanation of what?

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.

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.

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

@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.