How do to Interval timer

Here's an example of BlinkWithoutDelay with different on/off times. The new idea from the standard BWOD is to change the interval each time you switch from on to off and off to on.

const byte ledPin =  LED_BUILTIN;// the number of the LED pin
byte ledState = LOW;             

unsigned long previousMillis = 0;       
unsigned long intervalOn = 1000; //use 3000 for three seconds on         
unsigned long intervalOff= 5000;//use 3600000 for one hour off
unsigned long interval = intervalOff; //start off

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

void loop() {
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
      interval = intervalOn;
    } else {
      ledState = LOW;
      interval = intervalOff;
    }
    digitalWrite(ledPin, ledState);
  }
}
1 Like