One of the common questions related to using the millis() function in Arduino, is around timed events. After an event occurs, you want the code to wait for some time before doing the next step. But you don’t want to stop the program with delay().
[featured-image link=”https://www.baldengineer.com/use-millis-with-buttons-to-delay-events.html” single_newwindow=”false”]In this example, we will use millis() to wait a few seconds after a pushbutton press to turn on an LED. Then a few seconds later, we will turn it off. All without using delay().
Understanding millis()
The Arduino millis() function will let you accomplish this delayed action relatively easily. First, read through my multitasking with millis() tutorial and then look at some of my millis() cookbook examples Iâve already posted. Getting used to seeing this kind of code will make this line by line tutorial on timed events using millis() easier to follow.