Arduino basic ep.1: Replacing delay() with millis()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
most of the Arduino tutorial I found in the Internet escalate too quickly from LED blink directly to other actuators like servo several segments LCDC registers and so on that's a bit too fast isn't it we can actually learn a lot about basic Arduino programming even just from LED in this series we will learn about some Arduino programming only by using some LED and an IR remote the basic structure of an Arduino sketch always consists of a setup function which run only once in the beginning and loop function which run repeatedly and infinitely until we turn it off let's take the example of the blink sketch in the loop they repeat four processes turn the LED on then delay or in other words pausing the loop then turn it off and then pulse again and turn it on pause and so on the problem with the delay function is that it's usage is very restrictive let's say we want to add a button which function is to stop the blink the way a button or any sensor works in an Arduino is by monitoring the status of the button during the loop however when a delay happens all processes in the Arduino are post so the Arduino will not know that the button is pressed so practically the button becomes useless and we can imagine if the delay function cannot be used in such a simple circuit then how can we expect it to work in a more complex circuit so that's why we will use the Millis function in the loop the Millis function provides the amount of time that has passed since our Arduino sketch run we will use the blink LED again as an example let's say we want the LED to stay on for 500 milliseconds and it will be off for 300 milliseconds before it turns on again basically there are still two main actions which are turning LED on and turning it off the main difference is that we do not trigger these two actions directly but it will depend on the time the Arduino will run and it will watch the time by using the meliss function when the time satisfies certain condition it will then trigger the appropriate action let's set the first condition if T equals zero turn the LED on the second condition is if 500 milliseconds have passed this in it was turned on then turn it off the third condition is if 300 milliseconds have passed since it was turned off then turn it on as you can see here besides monitoring the time Arduino will also need to monitor the status of the LED whether it is on or off when it is on use this rule otherwise use this one so let's run this in T equals zero it will use this rule so the LED will be turned on and the time is marked then when the loop reach 100 milliseconds for example the LED is still on so this rule will be used but because it hasn't reached 500 it will not do anything 200 milliseconds still not do anything 300 400 and then at 500 the LED will be turned off then the time will be marked now the LED is off the loop will go on and keep checking whether we have reached 800 milliseconds or not at 600 nothing happened 700 and then at 800 it will be turned on then at a thousand 300 it will turn off again and then turn on at thousand 600 and so on now this is the part where the method is very useful let's add a button to stop the blink the only thing we need to do is to let Arduino monitor the status of this button and that's it because the loop never stops whenever we press the button Arduino will directly respond to it I will explain about how a button or a remote or any sensors can stop the blink in other video but for now I hope you understand the advantage of the meliss function compared to the delay function you
Info
Channel: Leonard AB
Views: 47,581
Rating: 4.7324634 out of 5
Keywords: arduino, millis, LED, basic, sketch, tutorial, beginner
Id: SOf7e5qeEyI
Channel Id: undefined
Length: 6min 5sec (365 seconds)
Published: Wed May 25 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.