How to make an LED blinks in 5s

hello everyone !

I want to make an LED blinks ON=OFF=500ms for 5s. And i have this coding but it didn’t work. Anyone can tell me where did i wrong ?

Thanks

const int LED_MOTOR1 =  A4;// the number of the LED pin

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

void setup() {
  // set the digital pin as output:
  pinMode(LED_MOTOR1, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();
  int i=0;
  while (i < 10 ){
    if (currentMillis - previousMillis >= 500) {
      previousMillis = currentMillis;
      if (digitalRead(LED_MOTOR1) == LOW) {
        digitalWrite(LED_MOTOR1, HIGH);
      } else {
        digitalWrite(LED_MOTOR1, LOW);
      }
    } 
  }
  i = i + 1; 
}

I suppose the first thing to note is your loop variable i will never get to 10 to meet the exit condition because it is incremented outside your loop.

Also, do you need to use an analog pin?

Also, currentMillis needs to be updated within the same loop, as currently it’s only being updated at the very start.

1 Like

You are incrementing i outside your while loop, so it will never exit.

1 Like

So, having i incremented outside the loop means it will never exit, and having currentMillis updated outside the loop means the state of the LED will never change.

1 Like

You increment ‘i’ outside the while loop so the loop will never end.
You put the whole thing inside loop() so, even if you fix the above problem, it will repeat forever. That is what loop() does.

// I want to make an LED blinks ON=OFF=500ms for 5s.
// And i have this coding but it didn’t work.
// Anyone can tell me where did i wrong ?

const int LED_MOTOR1 =  A4;// the number of the LED pin

void setup()
{
  // set the digital pin as output:
  pinMode(LED_MOTOR1, OUTPUT);

  // for 5 seconds
  for (int i = 0; i < 5; i++)
  {
    // half secoind on, half second off
    digitalWrite(LED_MOTOR1, HIGH);
    delay(500);
    digitalWrite(LED_MOTOR1, LOW);
    delay(500);
  }
}

void loop() {}
1 Like

@johnwasser seumim already started using millis() instead of delay(). Don’t teach him that evil stuff again. :stuck_out_tongue_winking_eye:

@seumim Try to avoid while. This will stop the loop from running. You could use a second millis() timer and measure 5 seconds. Instead of repeating after 5 seconds it would stop.
Move the code to separate functions to keep it clean and reuse variable names. You can use a static variable inside the functions instead of global variables. This allows you to reuse the previousMillis variable name.

Example for static variable (click to show)
function1()
{
static uint32_t previousMillis = 0;  // variable only valid inside function1 but retains value between calls
...
}

function2()
{
static uint32_t previousMillis = 0;  // variable only valid inside function2
...
}
1 Like

You could try this:

const int LED_MOTOR1 =  12;             // the number of the LED pin

unsigned long previousMillis = 0;       // will store last time LED was updated
int ledState = 0;                       // store current state of LED

void setup() {
  // set the digital pin as output:
  pinMode(LED_MOTOR1, OUTPUT);
}

void loop() {
  // just run this for 5 seconds, then ignore it
  if (millis() <= 5000) {
    if (millis() - previousMillis >= 500) {
      previousMillis = millis();            // reset timer
      ledState = !ledState;                 // toggle the state
      digitalWrite(LED_MOTOR1, ledState);   // write the new state
    }  
  }
}

Explanation:

  • loop() runs repeatedly anyway so you don’t really need another loop inside it in this case.
  • The ledState variable is updated by toggling it from 1 to 0 and back again in each loop using the not operator (!).
  • The outer if condition just checks if the loop has been running for less than or equal to 5 seconds (this could possibly result in unpredictable results if the rest of the loop takes longer to run - I just wanted to do it because I really liked @Klaus_K’s idea - in this case you could just add 1 to a counter variable use that instead - I put that in an alternative code example below).
  • The inner if condition just compares the output of millis() directly. This is fine if you’re only using it once or twice.

Note: I changed the pin to digital pin 12 for my own convenience.

Safe version for use with longer loops:

const int LED_MOTOR1 =  12;             // the number of the LED pin

unsigned long previousMillis = 0;       // will store last time LED was updated
int ledState = 0;                       // store current state of LED
int flashCount = 0;                     // store number or flashes so far

void setup() {
  // set the digital pin as output:
  pinMode(LED_MOTOR1, OUTPUT);
}

void loop() {
  // just run this until the counter reaches 9, then ignore it
  if (flashCount < 10) {
    if (millis() - previousMillis >= 500) {
      previousMillis = millis();            // reset timer
      ledState = !ledState;                 // toggle the state
      flashCount += 1;                      // increment the flash count
      digitalWrite(LED_MOTOR1, ledState);   // write the new state
    }  
  }
}
1 Like

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