Tutorial: How to use millis | Goodbye delay, Hello millis

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
most of us started learning the Arduino with the use of blink example which can be found in the Arduino IDE so now I will open link example under the pile examples b6 and blink so the blink example uses the LED built-in which is connected to digital pin 13 so I also prepared a simple demo circuit here in the setup we set the direction of the LED built-in as output then in the loop function it basically turns on the LED built-in delay per 1000 milliseconds turn off the LED then delays or 1000 milliseconds then loop again so let's upload this for your appearance so then uploading for the first time you enjoy watching a blinking LED which blinks every 1000 milliseconds somehow you decided to advance your experience by reading input inputs like buttons reading analog values from potentiometer and sensors and maybe displaying it through serial monitor or LCD eventually you will encounter an obstacle where you need to do two or more tasks at the same time for example you are required to blink 3 LEDs with different interval let's say the first LED should blink every 3 seconds the second LED should blink every 5 seconds and the third LED should blink every seven seconds this is impossible to code by just merely using the delay function so you may decided to seek for help online in poems asking how the heck I can do two or more tasks at the same time common answer to this question could be by a dual-core microcontroller like ESP 32 or a single board computers like Raspberry Pi or some would say learn the blink without delay so let's open the blink without delay example under the examples Digital blink without delay so as you can see there are foreign or new to you like what the heck is Milly's so as a beginner this is difficult to understand though this is well documented here so let's try to dissect this so bling without delay example is taking the advantage of using Millie's here nihlus function returns a time values in milliseconds since the microcontroller boot up time here in the loop the current time is saved to the current Millie's here in the if statement it checks if the elapsed time is equal or greater than the set interval we said 1000 milliseconds interval the elapsed time is basically the difference between the current Millie's which is the current time returned by the mill is function minus the previous Millie's so initially the previous Millis is equal to zero but it will be updated when when the if statement becomes true and executed as you can see here if the if statement is not true or it is false this whole if statement will not be executed then it will loop again until such time that the current meal is the current meal is minus the previous Millis is greater than the set interval or more than 1000 milliseconds and the if statement will be executed the current time will be saved to previous release and the led State toggle and it will be returned to the LED pin then it will loop again let's upload this blink without delay upload done uploading as you can see the result is the same as the blink example but this time blinking of LED is achieved without using the delay function as you can see there is no delay inside the loop function delay function is easy and simple to use but it is a blocking code where the microcontroller is doing nothing for the whole duration to illustrate that I prepare here a circuit and arduino sketch so let me first upload this one then uploading so currently the display is switched one to delay and you can press s2 or switch to so when we press the switch to switch to is press as you can see in the LCD s2 is press when we press s1 it will display that the switch one is press and it will delay for 5 seconds as you can observe when s1 is pressed and the pipe seconds of delay is executed during the whole duration of the delay the microcontroller is held to do nothing so I will press the switch 1 and I will press the switch 2 it will not do anything until such time that the delay under the switch one is complete so when the delay is complete you can press the switch to on the other hand Milly's function is little bit difficult to use but it is a nun blocking code where you can do multiple tasks built while at the same time I say virtually because microcontrollers process in sequential mode for example let's say you have Drita's in a row test1 test2 and test3 microcontroller will execute it worse thus one then touched to dentistry but not at the same time it is only executed virtually at the same time because of the microcontrollers speed of processing so let's solve the pending problem blinking the three LEDs with different intervals in here I prepared arduino sketch in the test circuit let's upload it and see the results so then uploading and as you can see the three LEDs are blinking in different rates this is a chip of course by using the me list function so let's see the source code so in here I defined the LEDs pin assignment so I connected put in a zero two eight two then I make the variables for the red LED for the yellow LED and the blue LED and as you can observe this is basically modified version of the blink without delay example processing the three LEDs blinking state so in here the current me lists for all issues to save the current time using the Milly's function so i make a note here it used a single carbon time for synchronization with other functions so virtually process all the tasks at the same time but it's not the same because it is process sequentially so process the red purse then the yellow LED then the blue LED so inside every process of the LED it's the blink without delay so we just modified it a little so that we can blink with different intervals for the LED and as you can observe there is also no delay here I hope you find it useful and helpful if you have some question please do not hesitate to write it in the comment box give me thumbs up and share these to your friends please do not forget to subscribe thank you and have a good day my name is Jorge signing up bye you
Info
Channel: TechToTinker
Views: 13,965
Rating: undefined out of 5
Keywords: millis function, arduino millis, millis, BlinkWithoutDelay, multitasking
Id: AEy3UDKNCLw
Channel Id: undefined
Length: 12min 16sec (736 seconds)
Published: Mon Jul 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.