/*
Blink Without Delay - with different intervals
2024-02-25: https://forum.arduino.cc/t/turn-on-solenoid-for-certain-duration-every-hour/1228168/4 code in thread
by noiasca
*/
const uint8_t ledPin = LED_BUILTIN; // the number of the LED pin
const uint32_t intervalOn = 500UL; // interval at which to blink (milliseconds)
const uint32_t intervalOff = 3000UL;
uint32_t interval = intervalOn; // will be changed during runtime
uint32_t previousMillis = 0; // will store last time LED was updated
void timerBlink() {
uint32_t currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // save the last time you blinked the LED
// if the LED is off turn it on and vice-versa:
if (digitalRead(ledPin) == LOW) {
digitalWrite(ledPin, HIGH);
interval = intervalOn; // the interval for the next iteration
} else {
digitalWrite(ledPin, LOW);
interval = intervalOff; // the interval for the next iteration
}
}
}
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
timerBlink();
// do other things non-blocking here
}