How to do multiple tasks in Arduino | Beginners | millis() function

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone have you ever felt difficulties  while using arduino and trying to do many tasks   at the same time if yes then this video  is for you this video will teach you how   to do multitasking with arduino let's say you  want to execute several tasks at the same time   for example reading data from the serial monitor  and blinking some leds monitoring some external   sensors sending data to the server after some  time period and blah blah here things turns   out to be a little bit complicated why why is  it complicated the problem is that arduino is   not built to multitask it at the same time i know  it's heartbreaking it is only capable of executing   one instruction at a time arduino does  not have multi-core processors like any   other classic computers you have so you cannot  really use the real parallel programming here   but what we can do is we can do some tricks that  can make you feel like you are doing multitasking   so without discussing any further let's move to  the main focus of the video how to do multitasking   for this let's make an example project where  we have to do four tasks at the same time   don't be confused the same time actually means  just giving you the feeling of the same time okay   so the first task is task 1 blinking an led every  one second and let's suppose it to be green led   and another task task 2 is growing  an led whenever the button is pressed   let's suppose the led to be red and task 3 is controlling the brightness of led by the   user input so let's suppose it to be blue led and  the user will provide input of a number from 0 to   255 where 0 means totally off and 255 means full  brightness and the last task is task 4   printing brightness of led after every 5  seconds so up to now we have defined our project for doing multitasking what we need to take  care about is arduino runs code one by one   we don't want to get stuck  in one task for a long time   we want to rapidly cycle over talks without  waiting at one place so to make it clear   let's just try to perform task 1 and task 2 in  arduino code for task 1 let's define pin 13   for green led and for task 2 let's define pin to  val for the red led and pin 4 for the push button   now let's configure the led1 and led2 as  output and button as input pull up here   button is connected to the ground when  pressed arduino reads it as 0 or else as 1.   now let's make a blinking code for task 1 waiting  for a second turning on the led again waiting for   a second and turning off the led and that's it  that's the blinking code and for the task 2 when   digitalRead() is high it means that the button is  not pressed so we need to turn off the red led   if digitalRead() is low it means that the  button is pressed so we need to turn the led on   the hardware arrangement is like this let's  upload and check if it works okay here you can see that the green led is blinking but  whenever i press the button red led is not going so what must be the problem we press the button  but the ordinary is still in task 1. to read the   button press we need the arduino to be executing  this code but when we press the button it's still   busy executing task 1. an arduino won't be able to  read the bottom presses okay that is why i say we   need to cycle over task rapidly so task 1 is really  making a problem for multitasking just because our   program is paused there in our project. Are there  any other tasks that may pause the program flow ?  yes there is it's task 4 we need to wait  for five seconds and print brightness right   so we now know that task 1 and  task 4 needs to be reformed   so that it won't pass the program flow  we can do that by using millis function   so what is millis function it counts the time in  milliseconds since you are in a power-up or reset   we can use it instead of delay to  see how let's check this code out   here in the code previous time saves the time  when the arduino is powered on and then goes to   loop function and current time saves the current  time count after the arduino is powered on   current time increases every millisecond when  current time is greater than the previous time   by 1000 millisecond then do something function  is called and the previous time will replace the   value with the current time again whenever current  time is greater than the previous time by 1000   millisecond then again do something is called and  again previous time will be the current time this   cycle goes on the something function runs after  every one second without pausing the loop cycle   now we know how to use millis function let's  use it in our project code let's define pin 11   for green led used for task 3 as we use green led  for brightness control we have to use pwm pin   so we choose pin 11 for it and make previous time  variable for t1 and t4 also let's define interval   for t1 and t4 we use 1000 for T1 because we need the led1  to blink every one second also we use 5000 for   t2 because sorry it's t4 because we need to print  brightness for led 3 every 5 seconds now in setup   we will initialize the serial communication with  9600 baud rate and also configure led3 as output   now it's time for writing task for task 1 we will  change the code with millis code we used before   now replace to something with digitalWrite(led1 , led1_state); whenever this if condition becomes   true we want led1 state to be just opposite of  the one it was before for that it led 1 state   equals complement of led1 state since  we are using led1 state variable   we need to define it let's go to the top of  the program and define led1 state equals LOW   let's also replace previous time with  this and also interval 1000 with this   leave the task 2 as it is because it is not  affecting the loop cycle also for task 3 if   data is available in serial buffer read the  input as integer and store it as brightness   we will make brightness as global variable  because we will need it in task four   if brightness is between 0 and 255 we  will write led 3 with brightness value   again for task 4 we will use millis function  as it is to be executed every 5 seconds   so let's paste the code we have used in millis  explanation and replace do something with serial   printing code for brightness let's also replace  previous time with this and interval 1000 with   this now we are done now our loop function  will not be paused by task execution time may   affect a little bit but that's pretty  good let's upload it to the arduino board the final hardware arrangement is like this i hope  you can make this circuit easily in the breadboard   let's check it out green led is  blinking which is task 1 and it's fine   also let's check task 2. whenever i press button  red led glows without affecting any other tasks it   is also fine now let's check uh task three for that  first go to the serial monitor and at the bottom   choose no line feed because we want only the  input data and not any other characters out there   type 100 and hit enter now blue led glows  with the brightness of about 40 percent   also you see tasks four right you can  see task four in serial monitor brightness   is printed every five second again type 255 and  hit enter blue led close with full brightness   okay fine Task 3 and Task 4 are also working with  our problem so that's it we did it you better   try with something more complex like controlling  seven segment display, servos or whatever you like   just the same concept applies out there  maybe with a little bit more tweets
Info
Channel: Amrit Aryal
Views: 9,752
Rating: 4.916914 out of 5
Keywords: arduino tutorial, arduino, multiple tasks in arduino, arduino multitasking, arduino millis multitasking, arduino multitasking programming, multitasking in arduino
Id: Ol6x5mjeu4w
Channel Id: undefined
Length: 10min 22sec (622 seconds)
Published: Mon May 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.