Arduino delay() and millis() Functions: Tight Loops and Blocking Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I hope you're doing fantastic let's say you're making a project with Arduino and you want some event to happen at a timed interval maybe every three seconds you want a servo to move or maybe every minute you want to send a status update to a web server whatever it may be you want something to occur at a timed interval how do you do that if your answer is using the delay function well you're kind of right but there is an other way and that's using the Arduino Millis function and the more familiar you are with using the Millis function to help you time events in your Arduino code the easier it will be to incorporate other parts into your program later on plus you'll be happier more cheerful and gosh darnit people like you now this is the second part in a series about using the Millis function in the first part we described what the Millis function is by the end of this lesson you'll get a better sense of why the delay function can start to cause issues in your code specifically in this lesson we'll talk about tight loops blocking code and an ancient recipe for inspiring music all right well I'm gonna take you on a little journey and this is the first part of that journey where I ultimately want to end up is I want you to be able to use the Arduino Millis function in your code so that you can create timed repetitive events so let's go ahead and start this journey and what I want to do is talk about a couple concepts that I will think help us understand the why and the how of doing what we want to do so we're going to start with the concept of a tight loop and when I say a tight loop what does that mean so let's take a look at an Arduino sketch to kind of demonstrate what I mean by a tight loop so here we are in an Arduino sketch and we've only got two functions void setup and void loop and as you may know voids only runs once and then it hands the show over to void loop and what void loop does is it goes through every line of code that might be inside the loop inside these curly brackets and it executes the first line then it executes the second and then the third and so on and so forth until it gets to the bottom and then it goes back to the top and it does that as fast as it can now how fast can it actually do that well it depends on which Arduino boards are using but nerd we know uno has a clock speed of 16 megahertz so that means that 16 million instructions are happening every second on the earth we know now each line of code isn't necessarily one instruction in fact it's most likely that it's multiple instructions but still that's pretty fast so let me get rid of these comments real quick and I have a question for you is this sketch right here the one where I have no code in this loop is this a tight loop that is is the time from the start of the loop to the end of the loop is that fast so the answer here is yes this is about it's tight of a loop we can get and that is because there's literally nothing inside the loop that gets executed so the time between the start of the loop at the top and it getting to the bottom it's like practically zilch so this is a very tight loop now let's add a couple lines of code what I'm going to do is start serial communication and then I'm going to print something to the serial monitor window so let's do that okay so now in the loop function I have one line of code and I'm printing something to the serial monitor window and I'm gonna ask you a question again is this a tight loop that is from the start of the loop to the end of the loop does that take a lot of time so we still have a pretty tight loop here we've got one line of code now it's worth noting that this loop is not as tight as the previous example in the previous example we had no code so it was just racing through this loop now we've got a function here a serial print and so we are going to be taking time to print something to the serial monitor but for all intents and purposes this is still a pretty quick loop so now what I'm going to do is I'm going to add a little bit more code what I'm going to do is I'll have the program check to see if a button is pressed and if it is I'll have it write something to the serial monitor so let me do that now you okay so I've added some code I've declared a button and then I've got an if statement and we check to see if the button has been pressed that is is the voltage at pin five high and if it is then we print something else to the serial monitor window so if I press the button then it prints something different out to the serial monitor so I'm going to ask you again is this a tight loop so from the start of the loop to the end of the loop is that pretty quick so I would still say yes this is a pretty tight loop we've got one two three four four lines of code three four lines of code we're printing to the serial monitor and then we're just doing a quick check to see if a button is being pressed and if it is we print something out so I would say yes this is still a pretty tight loop now it's not as tight as the last two examples but all of this code is still happening pretty darn quick so let's look at one final example what I'm going to do is add a delay to this program using the Arduino delay function so that I don't have this printing to the serial monitor so frequently so let me do that okay so now I've added a delay to the loop and it's a thousand millisecond delay so that's one second now I want to know is this still a tight loop is the time from the start of the loop to the end of the loop is that a lot of time okay so you've probably guessed that no this is definitely not a tight loop I mean the code starts fast we do this serial print but then we get halted right there at the delay function it's like the whole program comes to a standstill and we need to wait for this delay code to finish when the Arduino gets to this line of code it's kind of like going to the grocery store you get into the twelve items or less line but then the guy in front of you pulls out his checkbook and he starts writing a check you know you're gonna be there for a minute it's the same deal here so this is not a tight loop the time from the start of the loop to the end of the loop is pretty significant especially compared to the last couple programs the order of magnitude of time is huge now one thing you might have started picking up on and I was trying to demonstrate it here is that this whole thing about tight loops is relative it all depends on your application if you need to check the status of a sensor every 10 millionth of a second then a program that has three lines of code may not be in tighten up at all it just depends and another point to make is that not all lines of code take the same amount of time to execute if you're calling a function that does a bunch of stuff like serial print for example then that one line of code may take a whole lot longer than 10 other lines of code so the tightness of a loop is a relative idea okay so when a program stops at some point when it takes some time to execute some code that code can be called blocking code and this is kind of like just a general term so here we've got the delay function and the delay function is acting as blocking code because none of the other code after delay can run so it's getting blocked but it doesn't just have to be the delay function let's take this code and let's get rid of delay but we'll add a for loop so I'll do that now okay so I've added a loop to this program and it prints out numbers to the serial port in some other text so how long does this loop run for well it's gonna run for a while it's got to go through a hundred iterations before it stops now this is gonna take some time to finish and what does that mean for the code after this for loop is it able to go well it's gotta wait because it's being blocked by the for loop so here we could say that this for loop is blocking other code it's acting as a block now here's a quick side note that I think is worth talking about this loop this for loop well it's also a loop it's nested inside the main loop and I'm gonna break my promise here and I'm gonna ask you again is this for loop is this a tight loop now before you answer let Miriam ffice eyes how I want you to think about this for one time through this for loop from the top to the bottom does it take a lot of time well not really it's only got two lines of code so this is a pretty tight loop but it's a tight loop that goes through a lot of iterations so even a tight loop if taken long enough can block our code okay that was a little bit of a deviation but anyway okay so we've talked about tight loops and we've talked about code that blocks or blocking code now I want to take a moment and just talk a little bit more about this delay function what have we established so far okay first we've said that the delay function decreases the tightness of a loop so if you've got a tight loop and you add the delay function well it's going to make it less of a tight loop that is the amount of time it takes to get from the start of the loop to the bottom that time is going to increase with the delay function so we also know that the delay function blocks code kind of goes hand in hand with what we just said so when the delay function is running it is going to block other code from running while it's going so we could be like man this delay function is a total slacker and it's never going to anything in our projects but you know for a lot of simple programs that we write delete function works fantastic it's simple to use it's really easy to spell and it does just what it says so we shouldn't necessarily ostracize delay from our programming toolbox we should just recognize that it's a simple programming function that can work in a lot of instances but there is a time when you start to run into issues and that has to do with the blocking effect that the delay function has in our program now in the next lesson in this series we're going to identify where this really becomes an issue you'll learn when it makes sense to use the delay function in a program and when it's time to switch to using the Millis function so let's do a quick review first we talked about the tightness of a loop and we said that the tightness of a loop is relative it depends on what your application is then we talked about blocking code or code that blocks and essentially it's a generic term we can give to code that's going to take some time to execute that's going to stop other parts of our program from running while it executes well I hope you enjoyed this lesson in the next part of this series we're going to continue our journey of learning how to use the Millis function to create timed repetitive events in our dueƱo code I'll see you there bye you
Info
Channel: Programming Electronics Academy
Views: 55,944
Rating: 4.817317 out of 5
Keywords: Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++, Programming Electronics Academy, millis, millis(), delay, delay(), tight loop, blocking code
Id: IyxY1uQyY9U
Channel Id: undefined
Length: 13min 0sec (780 seconds)
Published: Thu Mar 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.