Arduino Sketch with millis() instead of delay()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's say you're building a project using Arduino and you have an event that you want to have happen every so often maybe every three minutes you want a servo motor to move or maybe every twenty seconds you want to update a webserver with the most current sensor eating how do you create these timed events in Arduino that's the objective of this series of lessons that by the end you'll be able to program repetitive timed events using your Arduino now if you've watched the previous lessons we've talked about the Millis function kind of in general we've talked about tight loops and blocking code and some issues that arise when using the delay function now I want to talk about how we can use the Millis function to create simple timed events specifically we'll cover a quick review of the Millis function well think about the Millis timeline will use Millis to create once-off timed events will use Millis to create repetitive timed events and finally we'll talk about how black holes can alter space time let's start with a quick review of the Millis function I think the easiest way to review this function is just to take a look at it in a simple sketch so what I'm going to do is write a sketch that prints the value of Millis to the serial monitor window so here we are in the Arduino IDE and I'm going to begin in the setup and use the serial begin function to enable serial communication then in the loop I'm going to use the serial print line function and I want to print the value of Millis so every time through the loop this program will print the current value of the Millis function so if we load this sketch onto Arduino and open up the serial monitor window you'll see the value of Millis is increasing rapidly so what is this value that the Millis function returns well the Millis function returns the number of milliseconds that your Arduino board has been powered up so as soon as your Arduino board is powered up the Millis function begins tracking the number of milliseconds that have passed it starts at zero and it counts up and up until it gets to four billion and some change and then it overflows which is just a fancy way of saying that it starts counting back at zero and it would take 49 days for the Millis function to overflow so the number of Millis gets real big and it gets big pretty quickly now the way melissa is able to track the number of milliseconds that have passed is by using the timer counter module built into the integrated circuit that the Arduino uses in our Arduino code we don't have to start the clock or start Millis it starts all by itself in the background now if you want to learn more about how the Millis function works definitely check out the first lesson in the series so how can we conceptualize Millis so that we can use it to create timed repetitive events well let's start by thinking of Millis as moving along a timeline so the timeline starts at 0 and it goes all the way up to 4 billion and some change and again these numbers represent milliseconds so at any point during our program we can call the Millis function and find out exactly where we are on the timeline so let's go ahead and zoom in and take a look at the first 5 seconds of a program so this little dot represents where Millis is on the timeline and for the sake of explanation let's slow down that dot because I really can't talk that fast so as soon as we power up Arduino the Millis function starts counting so you can see the value is increasing moving to the right along the time axis now if we want to create timed repetitive events how could we use this timeline to our advantage what if I reached out ahead in time and I meet a little hatch line and I said when the value of Millis gets here do this thing encode it might look something like this so let's talk through this sketch from the point when powers first applied so powers applied we get into the loop and the first thing we do is check Millis since we just began the value of Millis is low it's less than the value of event one which was a thousand so this if statement condition is false and the code in the curly brackets doesn't run so this loop continues to execute over and over in the if statement keeps getting passed over because the condition is false but that really doesn't last long B before we know it the value of Millis has exceeded the value of the event one variable so now the if-statement condition is true and the code within the if statement begins to execute so now we've created a timed event using Millis but this really isn't exactly what we're after it might work for some applications like let's say you're building a radiation warning exposure timer and say you when you flip on a switch it turns on the arduino and when so much time has passed an indicator light is turned on so after a defined interval an LED turns on and then it just stays on now we could have used a delay function to achieve the same thing but what's nice with using Millis is that since we didn't use the delay function we can do other stuff in the loop maybe we can read a sensor or update a display or whatever the Millis function won't block other code from running like the delay function would furthermore if we wanted to we could add more events that trigger at later times so this is one way to use Millis but it's really not what we're after we want to create repetitive timed events for example every 30 seconds maybe we want to read the water temperature or every minute we want a servo motor to move left and then right again for these types of programs this simple use of Millis won't do it so let's go back to our timeline so here we are at the timeline again and I'm thinking what if we do this what if we still have our hash mark right we say when Millis gets to this value then we want to execute our event but now what we'll do is when the Millis value gets to this point and our event code runs what we'll do and part of our code is update the event time and so essentially what we're gonna do is move that hash mark down the timeline to the next point so every time we get to that hash mark we also update the hash mark for the next time around it's kind of like we're holding this carrot in front of the Millis function like here come and get it and then when the Millis function finally gets there we move it further down that's just kind of mean so let's try some code and see what this might look like so here we are in the Arduino IDE and the first thing I'm going to do is set the interval of the event so I've created a constant and I've named it event interval and the reason I have it is a constant is because the interval isn't going to change I want my event to happen every thousand milliseconds so now what I'm going to do is I'm going to create a variable that I'm going to use to update the time I'm going to call it previous time so let's do that so I've created previous time and it is an unsigned long and the reason these are unsigned Long's is because the value of Millis gets extremely large and we want to be able to hold that value in our variable I talked about this in one of the previous lessons so make sure to check out one of the earlier videos in this series to learn more about that now on this setup I'm going to go ahead and start serial communication using the serial begin function because in the loop I'll be sending some stuff to the serial monitor so I'll do that now now down in the loop what I'm going to do is create a variable and it's going to get updated every time through the loop with the current time and how do I get the current time well I just called the Millis function remember the Millis function is going to report back to me where we are on that Millis time line so I'll do that now so I've created a variable called current time and I set it equal to the output of the Millis function so every time through the loop current time is going to be holding the current value of the Millis function so now comes the key step I want to create some type of function that is only going to happen when I'm at my event interval so every 1000 seconds I want something to happen so the code I'm going to use is going to be an if statement and the condition here is what is key so what I'm going to do is go ahead and write this if statement and then we'll talk about it you okay so this kind of looks like a doozy but let's just talk through this so what is this if statement do well it takes the current time and remember the current time is constantly being updated through the loop with the Millis function so it takes the current time it subtracts it from the previous time and then it checks to see if the value here is greater than or equal to the event interval okay so let's just let's just really nail down these events write down let's put some numbers to them so we know what event interval is never going to change right it's a constant and we set it to 1000 so you can just think of this as 1,000 and now we've got two other numbers here we've got current time and we know this value is always going up right because this is the value that we're getting from Millis so think of current time is just it's almost like a big arrow up it's always getting bigger and bigger and bigger so we've got something that's getting really big and now we've got to ask what is previous time well previous time starts at 0 so we've got a number that's 0 here and then we've got a value here that's current time so the first time through the loop the difference between current time and previous time is going to be small and that's because we just started the program right so Millis you know it starts at 0 maybe Millis is at like a hundred so we'd say well 100 minus 0 that's 100 and we'd ask is 100 greater than or equal to a thousand no it's not so this if statement all the code inside here wouldn't run and so we'd get to the end of the loop and we'd start right back at the top we'd assign Millis the value of Millis to the current time so let's say now it's 200 milliseconds all right so now current time is 200 we get back to this if statement and now we ask all right is 200 minus 0 is that greater than or equal to a thousand it still isn't so we skipped this if statement we would keep doing that over and over until a second has passed when a second has passed then the Millis function is going to return a thousand so now we'd say is 1,000 minus zero is that greater than or equal to 1000 we'd say hey yes it is sweet so we can jump in to our code so the event code that I've got here is just printing something to the serial monitor and then what we do and this is kind of the the trick this is how we scootch down that hash mark so that the next time around we can have this event happen again in another thousand seconds so what we do is we take the previous time which was zero and we update it with the current time and we had said that the current time was a thousand so now previous time is now equal to a thousand okay so we've updated that time we exit the if statement and then we come right back into the loop now current times gonna get updated again right so maybe it's like I don't know a thousand let's just say a thousand and ten milliseconds so now we've got a thousand and ten here and we've got to subtract a thousand and ten from previous time well what was previous time well we had just updated it to a thousand so it's a thousand and ten minus 1000 well that's ten is ten greater than or equal to a thousand nope it's not so we skip over this code and you can kind of see what's going on here we're gonna keep skipping over this code until another second has passed because when another second has passed then Millis is going to be two thousand and we'll ask ourselves in this if statement is two thousand minus 1000 is that greater than or equal to one thousand we say yeah it's equal to that so then we can run this code again and you can see we'll print out to the serial monitor our event code or whatever code it is that you want to have happen at that specified time and then we update the previous time again and this right here is the coding paradigm that is going to allow you to create repetitive timed events using your dueño so I know I can look a little tricky seems like there's a lot of variables the best way to really get a handle on this is just to write the program yourself and mess around with it all right well let's just do a quick review of what we talked about first we did a quick review of the Millis function we said hey you don't have to like start the Millis function it's just always counting it starts at zero from when the time the Arduino is powered up and it counts up to four billion and some change and then it starts over again once it gets to the top then we talked about kind of thinking about Millis moving down a timeline in our program this is kind of a way to help us conceptualize how we can use Millis to create timed events then we talked about creating one soft timed events using the Millis function and finally we talked about creating repetitive timed events using the Millis function well hey I hope you found this lesson helpful we're going to have a couple more lessons about the Millis function and using it some different cases so stick around stay tuned for the next lesson have a great day take it easy bye you
Info
Channel: Programming Electronics Academy
Views: 110,074
Rating: 4.9571085 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, arduino uno, arduino projects, electronics, electronics repair, arduino uno programming, electronics for beginners, computer programming for beginners, electronics engineering, arduino uno tutorial for beginners, arduino millis reset, arduino programming
Id: BYKQ9rk0FEQ
Channel Id: undefined
Length: 14min 26sec (866 seconds)
Published: Thu Apr 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.