Have a loop run for X amount of time

Hi,

Perhaps you could clear one part up for me then. I'm not sure how one line works.

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

"unsigned long currentMillis = millis();"

unsigned long is a variable, currentMillis is the name of that variable. I get that much. but when you = it to millis(), doesn't that mean your setting currentMillis as a clock that keeps on counting till the counter maxes out and resets?

Then your saying if currentMillis (the counter that keeps on counting) - (minus) previousMillis (which was set to zero), is greater than interval (1000ms) then:
reset curentMillis to previousMillis which equals 0. So how can you reset currentMillis to 0 if currentMillis = millis().

I'll keep playing in the meantime :slight_smile:

Thanks again