Blink 5 Times - for loop

Hello, i combined the “BinkWithoutDelay” and a for loop. But it is not working. What did i wrong?

const int ledPin = 13;
int ledState = LOW;             // ledState used to set the LED
int flash = 5;

unsigned long previousMillis = 0;        // will store last time LED was updated

const long interval = 1000;           // interval at which to blink (milliseconds)


void setup() {
pinMode (ledPin, OUTPUT);
}

void loop() {
  
 
 
 for (int i=0; i<flash; i++){
  
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   


   // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
delay (5000);
}

Consider what happens in the for loop. It will run very fast, and each time through the checks for elapsed time will happen but so fast that there is no time for millis() to increase enough to trigger the change of LED state. Eventually after many iterations of loop() and the for loop the elapsed time will exceed the time period and the LED will change state.

At the end of the for loop the LED will either be on or off and will stay that way for 5 seconds because of the delay()

Thank you! But does it not wait until the difference is grater/equal the interval?

for (int i=0; i<flash; i++){

This line of code will increment every time through loop and will end long before any LED is triggered.

You need to have a variable to count the number of flashes and only increment that variable when a flash actually happens. When the 5 flashes are completed your code will know not to flash any more.

This is an example of a simple state machine. Yor variable will be keeping track of the state of the system - is it at flash 1 or at flash 3 etc.

…R

but does it not have to run thru the interval before it begins again the loop till flash reaches 5 counts?

Sorry for my insistence!

Do you want the flashes to stop after 5 on/off repetitions or to have 5 seconds between the flash sequences ?

Here is an example of a program with 2 states. When in state 0 the LED flashes on/off 5 times then changes to state 1. When in state 1 the LED is off and the program waits for 5 seconds then changes to state 0.

const byte ledPin = 13;
byte flashCount = 0;
byte state = 0;
unsigned long waitStart;
unsigned long shortPeriod = 500;
unsigned long longPeriod = 5000;
unsigned long currentTime;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  waitStart = millis();
}

void loop()
{
  switch (state)
  {
    case 0:
      currentTime = millis();
      if (currentTime - waitStart >= shortPeriod)
      {
        digitalWrite(ledPin, !digitalRead(ledPin));
        waitStart = currentTime;
        flashCount++;
        if (flashCount == 10)
        {
          state = 1;
        }
      }
      break;
      
    case 1:
      currentTime = millis();
      if (currentTime - waitStart >= longPeriod)
      {
        waitStart = currentTime;
        flashCount = 0;
        state = 0;
      }
      break;
  }
}

Thank you so much!

I made it that way bat why it will not let flash the second led?

const int led_ready = 51;
const int led_set = 52;
const int led_go = 53;
int ledState_ready = LOW;
int ledState_set = LOW;
int counter_ready = 0;
int counter_set = 0;                         // blink counter

unsigned long previousMillis = 0;            // will store last time LED was update
long interval = 1000;                        // interval at which to blink (milliseconds)


void setup() {
  pinMode (led_ready, OUTPUT);
  pinMode (led_set, OUTPUT);
  pinMode (led_go, OUTPUT);
}

void loop() {
  // blink LED ready
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    if (ledState_ready == LOW)
      ledState_ready = HIGH;
    else
      ledState_ready = LOW;

    counter_ready++;
  }
  if (counter_ready < 10) {
    digitalWrite(led_ready, ledState_ready);
  }

  // blink LED set
  if (currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    if (ledState_set == LOW)
      ledState_set = HIGH;
    else
      ledState_set = LOW;

    counter_set++;
  }
  if (counter_set < 10) {
    digitalWrite(led_set, ledState_set);
  }

}
  // blink LED set
  if (currentMillis - previousMillis > interval)

When you change the state of the ready LED you reset the value of previousMillis to the current time so this test will never return true because the interval between current and previous will never be long enough.

Yeah, I wasn't clear on what you were trying to do either. This assumes you want to flash 5 times, pause, and then do it again.

const int ledPin = 13;
int ledState = LOW;             // ledState used to set the LED
int flash = 10;                 // Flash LED 5 times--two passes for ON/OFF sequence

unsigned long previousMillis = 0;        // will store last time LED was updated

const long interval = 500;           // interval at which to blink (milliseconds)


void setup() {
  pinMode (ledPin, OUTPUT);
  previousMillis = millis();
}

void loop() {

  unsigned long currentMillis = millis();
  for (int i = 0; i < flash ; i++) {
    while ( (currentMillis - previousMillis) < interval) {
      currentMillis = millis();
    }
    previousMillis = currentMillis;
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
  ledState = LOW;
  digitalWrite(ledPin, ledState);

  delay (2000);
}

Maybe it is easier to describe what we want to do:

I am trying to make a stopwatch with a start-light (Traffic-light) for our "carrera race game" with my 9year old son.

We have a miniature traffic-light with 3 LEDs end we would like to have a sequence that makes flash the red light 5 times, then the orange light 5 times and then the green light 1 time and stay on for 10 seconds.

Thank all of you for letting be my sons hero!

const byte redLedPin = 13;
const byte orangeLedPin = 12;
const byte greenLedPin = 11;


byte flashCount = 0;
byte state = 0;
unsigned long waitStart;
unsigned long shortPeriod = 500;
unsigned long longPeriod = 5000;
unsigned long currentTime;

void setup()
{
  Serial.begin(115200);
  pinMode(redLedPin, OUTPUT);
  pinMode(orangeLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  digitalWrite(redLedPin, HIGH);
  digitalWrite(orangeLedPin, HIGH);  
  digitalWrite(greenLedPin, HIGH);
  waitStart = millis();
}

void loop()
{
  switch (state)
  {
    case 0:  //flash red led 5 times
      currentTime = millis();
      if (currentTime - waitStart >= shortPeriod)
      {
        digitalWrite(redLedPin, !digitalRead(redLedPin));
        waitStart = currentTime;
        flashCount++;
        if (flashCount == 10)
        {
          state = 1;
          flashCount = 0;
        }
      }
      break;

    case 1:  //flash orange led 5 times
      currentTime = millis();
      if (currentTime - waitStart >= shortPeriod)
      {
        digitalWrite(orangeLedPin, !digitalRead(orangeLedPin));
        waitStart = currentTime;
        flashCount++;
        if (flashCount == 10)
        {
          state = 2;
          digitalWrite(greenLedPin, LOW);
        }
      }
      break;

    case 2:    //green LED on for 5 seconds
      currentTime = millis();
      if (currentTime - waitStart >= longPeriod)
      {
        digitalWrite(greenLedPin, HIGH);
        state = 3;  //no code for state 3 so nothing will happen in this state
      }
      break;
  }
}

Thank all of you. I went UKHeliBobs way.

Thats what worked perfect for me. Some minor modifications and now it is like we imagined!

const byte redLedPin = 51;
const byte orangeLedPin = 52;
const byte greenLedPin = 53;


byte flashCount = 0;
byte state = 0;
unsigned long waitStart;
unsigned long shortPeriod = 500;
unsigned long longPeriod = 5000;
unsigned long currentTime;

void setup()
{
  Serial.begin(115200);
  pinMode(redLedPin, OUTPUT);
  pinMode(orangeLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  digitalWrite(redLedPin, HIGH);
  digitalWrite(orangeLedPin, HIGH);  
  digitalWrite(greenLedPin, HIGH);
  waitStart = millis();
}

void loop()
{
  switch (state)
  {
    case 0:  //flash red led 5 times
      currentTime = millis();
      if (currentTime - waitStart >= shortPeriod)
      {
        digitalWrite(redLedPin, !digitalRead(redLedPin));
        waitStart = currentTime;
        flashCount++;
        if (flashCount == 10)
        {
          state = 1;
          flashCount = 0;
        }
      }
      break;

    case 1:  //flash orange led 5 times
      currentTime = millis();
      if (currentTime - waitStart >= shortPeriod)
      {
        digitalWrite(orangeLedPin, !digitalRead(orangeLedPin));
        waitStart = currentTime;
        flashCount++;
        if (flashCount == 11)
        {
          state = 2;
          digitalWrite(orangeLedPin, HIGH);
          digitalWrite(greenLedPin, LOW);
        }
      }
      break;

    case 2:    //green LED on for 5 seconds
      currentTime = millis();
      if (currentTime - waitStart >= longPeriod)
      {
        digitalWrite(greenLedPin, HIGH);
        state = 3;  //no code for state 3 so nothing will happen in this state
      }
      break;
  }
}