I’m having trouble understanding how to use Millis to get my single LED to blink ON for 100ms, then OFF for 150ms, then ON for 100ms, then OFF for 1500ms.
Obviously this is easy using delay but I’m struggling with milli and wrapping my head around how to get it to work here. I don’t know how to “block” the code from turning the LED ON when I’m trying to wait for that 1500ms without actually blocking the code and using delay. Going through Blink without delay makes sense but not when I’m trying to use 4 different intervals for a single LED. What happens is since the code runs through the void loop without being blocked, it doesn’t wait for the 1500ms LED OFF and just goes back to the 100ms LED ON and the LED just blinks rapidly.
So my question is, when you have 4 different intervals for the same output, how do you get your milli code to actually wait for each interval because moving on to the next without using a blocking code?
DELAY doesn’t work for me because I’m using a push button and it won’t allow the push button to be registered unless I click it at the right moment.
you could represent interval duration as the elements of an array
so each element of the array will represent duration of one interval.
so you define a delay depending on the element of the array
then you iterate over elements of this array so there you get your N intervals delay.
all of this would be wrapped in a state machine (for the sake of programming) which is non blocking
learn how the "BlinkWithoutDelay" example works.
Do that as long as you can write such a code by heart.
Read post #3
or just use something premade:
/*
Rhythm LED
by noiasca
2021-12-14
*/
#include "Noiasca_LED.h" // copy file to sketch folder
RhythmPin rhythmLed {13}; // declare LED and assign pin
void setup() {
rhythmLed.begin(); // you have to call the .begin() method for the LED
// ON OFF ON OFF
rhythmLed.setInterval(100, 150, 100, 1500);
rhythmLed.on(); // you can switch the effect on
}
void loop() {
rhythmLed.update(); // you have to call update() for the LED
}
and put the included file in the folder of your sketch. Noiasca_LED.h (28.3 KB)