How to use millis() function to multitask in arduino code.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
sometimes in arduino code you want to perform certain actions in define intervals you would say it's easy use the delay function it pauses the program for a specific amount of time the use of the delay function however has one major flow it holds the execution of the entire sketch no other readings of sensors mathematical calculations or pin manipulation can be done while it's in progress in today's video we'll try to achieve the same result with mili's function we would make the blink sketch the most simple one around a bit more complicated and explain and implement multitasking in arduino code let's get started [Music] [Applause] [Music] [Applause] so what does miley's function do it returns the number of milliseconds that has passed since the current program was started this value would not grow forever it will reset back to 0 after approximately 50 days the important thing is that it returns value in unsigned long data type any calculation with smaller data types like integer can result in logic errors in the code let's first set up the most basic blink sketch with the use of the delay function led cathode goes via current limiting resistor to ground of arduino and led anode goes to digital pin 3. in the code digital pin 3 is declared as output and in the main loop we execute following actions we send high signal to digital pin 3 to let the led then we wait for 1 second and then we send low signal to turn the led off and again we wait one second since main loop is repeated over and over again led blinks the major flow of using delay in the code is the fact that it holds the execution of the entire sketch and no other actions can be executed let's send the sketch to the board and see if the led connected to digital pin 3 would blink perfect one of the topics of this video is multitasking multitasking is an ability to perform more than one task at the same time arduino can perform multiple tasks simultaneously and in a way that the tasks are pretty much independent from each other let's see if we can multitask and blink two leds with different frequency and if we can make it with the use of the delay function we need to add one more led this time connect it to digital pin 9. the objective for the program is to blink first led so that it is on for one second and then off for another second the second led is on for quarter of a second and then off for the same amount of time first we declare both pins as output then we send high signal to first led and then we send the sequence of high and low signals to led2 every 250 milliseconds after one second we send the low signal to the first led to turn it off and again we repeat the sequence of high and low signals sent to led2 let's see how this would work the code is confusing as the duration of led 1 being on and off is not specified implicitly but rather calculated as multiplication of delay values used for led 2. so what's going to happen if i increase the frequency of led2 blinking it affects the frequency of led1 so we do not have two independent tasks here but rather one just more complex one so before we look at real multitasking let's implement the blink sketch using millis instead of delay we would need few extra variables here first one keeps the number of milliseconds since the sketch was started so basically it holds the result of running mini's function next one keeps the number of milliseconds since the sketch was started until the time of the last led status change and the last one keeps the interval we want led to be on or off for in this case this interval is one second we declare the led pin and we also have one extra variable to keep the led state 1 being on and 0 being off in the main loop we update ms from start variable then we check the time since the last led status change exceeded 1 second if it did we update ms previous read led1 variable and change the status of led switching it on when it is off and vice versa let's see if we can blink the led connected to pin 3. yes we can now let's introduce the second led you see that we have a second set of led specific variables we declare two pins and two led states variables in the main loop we also save result from millis to ms from start variable and then we have two blocks of code similar to the one from a previous example one for led1 and its unique frequency and the other one for led2 and its frequency different from the other one let's put it to the test we start with the original frequencies then we increase the frequency of a second led and you can see that the first led is not impacted finally we increase the frequency of the first led 10 times and that also works perfectly till now the frequencies were hard coded let's try to change them dynamically to prove that both tasks are truly independent as they should be in a multitasking scenario we'll introduce two potentiometers connected to analog pins 1 and 7. the readings from those potentiometers would be mapped to the interval range from 100 milliseconds to 1000 milliseconds and this way we would be able to change led frequencies on the fly we would be using the code from the last example with few additional sections we need to declare two potentiometer pins they would be declared as analog input then in mainloop we would initially reset the intervals of both leds to zero and then in a for loop performing five times analog read from both potentiometers we would sum the values for each led and then divide the result by 5. this way we would get the moving average value the potentiometer reads are sometimes all over the place so this way we are getting more precise read of potentiometers current position the rest of the code remains unchanged let's send the sketch to the boat i will also send interval values to the serial monitor so you can observe how they change at first both leds blink with interval values equal 1 second let's start with increasing frequency of the first led now let's do the same for the second one and now let's increase frequency of both leds together you can see that both tasks are truly independent you even have a third task of outputting values to the serial monitor that is independent and not impacted by the other two this is all for today what is the conclusion from this video that even though the delay function is easy to use it has significant drawbacks knowledgeable programmers usually avoid the use of a delay function for timing of events longer than tens of milliseconds unless arduino sketch is very simple i definitely would think twice before i use delay in my future sketches and look for millie's alternative if you got that far into my video that means you enjoyed the content so click the like button subscribe to my channel not to miss any future videos also please consider supporting me either through paypal or my patreon webpage i will see you in my next video [Applause] you
Info
Channel: Mario's Ideas
Views: 56,091
Rating: undefined out of 5
Keywords: arduino, nano, arduino nano, millis, potentiometer, led, multitasking, delay, tutorial, howto, simple project, arduino project
Id: dCku8BabPx4
Channel Id: undefined
Length: 10min 33sec (633 seconds)
Published: Fri Oct 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.