`I was wondering if I can make an arduino to blink an LED at an inequal interval. I tried searching but I could not find anything like that. Example code works with an equal interval, like a 1000ms and can be changed.
For delay function, it is easy to change the inequal interval like shown below but delay will stop other codes and wait.
Easy.
Just change interval every time you changed the LED state:
unsigned long currentMillis = millis();
unsigned long interval = 1000;
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;
interval = 200;
} else {
ledState = LOW;
interval = 5000;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
unsigned long currentMillis = millis();
unsigned long previousMillis = millis();
unsigned long interval[] = {5000,200};
constexpr uint8_t ledPin = LED_BUILTIN;
uint16_t ledState=LOW;
void setup()
{
pinMode (ledPin, OUTPUT);
}
void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis >= interval[ledState])
{
// 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);
}
}
YAM (yet another mousetrap) YAB (yet another blink-without-delay)
byte ledPin = LED_BUILTIN;
int ledON = 200, ledOFF = 5000; // LED state intervals
unsigned long timer;
void setup() {
pinMode (ledPin, OUTPUT);
}
void loop() {
if (millis() - timer >= (digitalRead(ledPin) == 0 ? ledOFF : ledON)) { // read pin state, set interval
timer = millis(); // set new timer for next interval
digitalWrite(ledPin, !digitalRead(ledPin)); // set LED pin state to opposite (!) current pin state
}
}
... I put various bits of what I learned on this Forum from you lot. I don't understand a lot of your (collective) solutions, but I try to read and learn (some stuff I just do not have the capacity to comprehend). Thank you for showing your many different ideas. It is loads of fun.