How to Execute a Timed LED On/Off Sequence Using millis() Function

Hello ,I’m struggling with a problem. I want to execute a sequence of turning an LED on and off, but not just a simple on/off; I want a timed sequence that turns the LED on and off at specific intervals. My requirement is to use millis() because other actions need to run in parallel. Here’s what I’ve done:
(currentMillis - timer2 >= 1000 && currentMillis - timer2 < 1100) ? digitalWrite(motor, LOW) : digitalWrite(motor, HIGH);
(currentMillis - timer2 >= 1100 && currentMillis - timer2 < 1500) ? digitalWrite(motor, HIGH) : digitalWrite(motor, LOW);
(currentMillis - timer2 >= 1500 && currentMillis - timer2 < 2000) ? digitalWrite(motor, LOW) : digitalWrite(motor, HIGH);
(currentMillis - timer2 >= 2000 && currentMillis - timer2 < 3000) ? digitalWrite(motor, HIGH) : digitalWrite(motor, LOW);
(currentMillis - timer2 >= 3000 && currentMillis - timer2 < 5000) ? digitalWrite(motor, LOW) : digitalWrite(motor, HIGH);
(currentMillis - timer2 >= 1000 && currentMillis - timer2 < 1100) ? digitalWrite(motor, LOW) : digitalWrite(motor, HIGH);
(currentMillis - timer2 >= 1100 && currentMillis - timer2 < 1500) ? digitalWrite(motor, HIGH) : digitalWrite(motor, LOW);
(currentMillis - timer2 >= 1500 && currentMillis - timer2 < 2000) ? digitalWrite(motor, LOW) : digitalWrite(motor, HIGH);
(currentMillis - timer2 >= 2000 && currentMillis - timer2 < 3000) ? digitalWrite(motor, HIGH) : digitalWrite(motor, LOW);
(currentMillis - timer2 >= 3000 && currentMillis - timer2 < 5000) ? digitalWrite(motor, LOW) : digitalWrite(motor, HIGH);
But this isn’t working as expected. Sequentially, it should look like: - from 0 to 1000 ms = off - from 1000 to 1100 ms = on - from 1100 to 1200 ms = off - from 1200 to 1500 ms = on - etc. Where am I going wrong?🤔
1 Reply
Enthernet Code
Enthernet Code2mo ago
@Camila_99$$ Hello I see what you’re trying to achieve, and it’s great that you’re using millis() for non-blocking timing to allow other actions to run in parallel. However, there seems to be a logic issue in your code with the intervals and the use of the ternary operators. The conditions are overlapping and might be causing unexpected behavior. Try this out
unsigned long currentMillis = millis(); // Get the current time
unsigned long interval = currentMillis - timer2; // Calculate the time elapsed

if (interval < 1000) {
digitalWrite(motor, LOW); // 0 - 1000 ms: off
} else if (interval < 1100) {
digitalWrite(motor, HIGH); // 1000 - 1100 ms: on
} else if (interval < 1200) {
digitalWrite(motor, LOW); // 1100 - 1200 ms: off
} else if (interval < 1500) {
digitalWrite(motor, HIGH); // 1200 - 1500 ms: on
} else if (interval < 1600) {
digitalWrite(motor, LOW); // 1500 - 1600 ms: off
} else if (interval < 2000) {
digitalWrite(motor, HIGH); // 1600 - 2000 ms: on
} else {
timer2 = millis(); // Reset timer2 when the sequence ends (>= 2000 ms)
}
unsigned long currentMillis = millis(); // Get the current time
unsigned long interval = currentMillis - timer2; // Calculate the time elapsed

if (interval < 1000) {
digitalWrite(motor, LOW); // 0 - 1000 ms: off
} else if (interval < 1100) {
digitalWrite(motor, HIGH); // 1000 - 1100 ms: on
} else if (interval < 1200) {
digitalWrite(motor, LOW); // 1100 - 1200 ms: off
} else if (interval < 1500) {
digitalWrite(motor, HIGH); // 1200 - 1500 ms: on
} else if (interval < 1600) {
digitalWrite(motor, LOW); // 1500 - 1600 ms: off
} else if (interval < 2000) {
digitalWrite(motor, HIGH); // 1600 - 2000 ms: on
} else {
timer2 = millis(); // Reset timer2 when the sequence ends (>= 2000 ms)
}
Want results from more Discord servers?
Add your server