Arduino millis() function: 5+ things to consider

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I hope you're doing fantastic have you heard of the Arduino Millis function did you know that it gives you access to the arduino z' internal timer counter hardware that can be used for the timing of different events in this lesson we're going to take a detailed look at what the arduino Millis function is specifically we're going to talk about a hardware clock a timer counter module how to actually get the value from the Millis function some things to consider when storing the value of the Millis function and also some things to consider when doing math on those values and finally we'll talk about the tensile strength of a rubberband so what is the Millis function to answer this question we need to first learn what a hardware clock is and also what a timer counter is so just hang with me for a moment as we talked briefly about hardware clocks and timer counters so generally speaking a hardware clock is an electrical circuit that generates a signal like a voltage pulse at a consistent frequency now there's a lot of different circuits that can generate voltage signals some are really precise some you can make from like discrete components that you might hook up on a breadboard and others are built into modules that we can connect to our Arduino now the integrated circuit that nerd we know uses has a built in Hardware clock which is pretty cool but most Arduino boards have an onboard crystal oscillator circuit it's this oscillator circuit that is used as the clock source for the Arduino now keep in mind a clock source isn't like a wristwatch saying hey it's 6:00 a.m. time to go get some coffee a clock is simply generating a consistent signal will call that signal a tick so if we want to time events a clock input only gets us so far what we really want to be able to do is count the number of ticks and then at any point in time be able to read how much time has passed since that clock started counting lucky for us the integrated circuit that the Arduino uses already has this piece of hardware built right into it it's called a timer counter module a timer counter module can do a bunch of stuff but for the sake of this discussion about the Millis function we're interested in a couple specific features the first is its ability to count the clock ticks and the second is to keep a running tally of those clock ticks so a timer counter is able to count clock ticks it starts counting as soon as powers applied to the Arduino and it doesn't stop it's always counting and you don't even have to tell it to start in the sketch it just goes and it starts counting at zero and it comes up and up and up until it gets to its maximum count and then it starts over and it begins counting again at zero so how long can account for hey that's a good question it can count up for 49 days until it starts over again so some of the jargon for that timer counter starting over is called rolling over that is the timer rolls over but a more proper way to say it is that the timer overflows once I sat and waited for 49 days to see if I could see the timer roll over for 49 days I didn't eat or drink except like coffee and donuts and crab cakes with really delicious seafood sauce but anyway so what is this timer and counter stuff have to do with the Arduino Millis function so here's the deal the Millis function gives us access to the running tally that the timer counter has been keeping track of when we call the Millis function it tells us the current value of the counter in milliseconds so said another way the value that is returned by Millis is the amount of time that has passed since the Arduino board was plugged in you know like since it had power applied to it so what do I mean returned by okay well let's just do a quick review of some jargon around Arduino functions there's two words I want to talk about call and return so when you type a function out that is when you use it in your code in a sketch you're said to be calling the function or you could say it's a function call so when I type out in my sketch I am calling the Millis function now if a function performs a calculation for you and it gives you some information back it's said to return a value you might hear something like what value does that function return so when I say that the Millis function returns a value what I'm saying is that it gives us the current time in milliseconds that the Arduino has been running so how do you actually get that value well to get the value you can call the Millis function and then set the returned value equal to a variable so here I've got a variable named previous time and I set it equal to the output of the Millis function so every time the Arduino goes through the loop this variable is going to be updated with the most recent count of time in milliseconds so after one minute of being powered up the Millis function would return the number 60,000 since every second has a thousand milliseconds and every minute has 60 seconds so there'd be 1,000 times 60 if you waited seven days the number returned by Millis would be over 600 million another way to get the value of Millis is to call the function inside of a condition so for example let's say we have an if statement now an if statement has a condition and if the condition is true then we execute the code in the if statement but if the condition is false then we skip that code so we can directly use the Millis function inside of a condition like this so when that condition gets evaluated the Millis function checks in with a timer counter and then it returns the current count in milliseconds and it dynamically updates every time this condition is checked so for me it kind of helps me think of that Millis call that function call I just kind of think about it as a number as opposed to being like a function it helps me think through it so those are two ways that you can get the counter timer value using the Millis function getting those values doesn't actually mean that we know how to use them so in another lesson I'll discuss how we actually use the Millis function in a way that helps you out a ton especially when you start running into issues using the Arduino delay function which can happen pretty darn quick but before we call this lesson quits I want to highlight one more thing about the Melos function you may have noticed that the value that the Millis function returns can get really big really quick the biggest value that it can return it's like over four billion and some change so practically what that means for us is that if we're going to store that number in a variable we need to make sure that we use a datatype for that variable that has space for it so as a quick refresher a variable it's like a storage locker it can only fit so much stuff if you're trying to stick like dirty socks an old typewriter in 74 years worth of Vogue magazine into a tiny little gym locker it's just not gonna fit the same goes with variables if you use a variable that's too small and try to store a really big number in it some funky stuff is gonna happen the data type of a variable is what specifies the kind of data in the size of the data that a variable will be storing so with an Arduino Uno you've got a bunch of data type options we've got bytes but those only hold a number from 0 to 255 so that's way too small we've got integers those will hold a value from negative 32,000 up to positive 32,000 and some change but as we saw that's still really too small we could fill that up in a matter of seconds well there's also floats and those can hold a really big number but generally they're supposed to be used for numbers with decimal points so it doesn't make it a lot of sense to use those but we do have a really big data type on an Arduino Uno that's called a long and it's 32 bytes and that's a number from negative 2 billion and some change to positive 2 billion and some change so now we're talking that seems pretty big but we can make it even bigger by default a long data type will hold negative values but since we know that the value that Millis returns is always going to be positive then it seems like kind of a waste lucky for us we can use an unsigned long and what that allows us to do is to shift the storage capacity to the positive numbers and so now it allows us to hold a value from 0 all the way up to 4 billion and some change which is perfect especially since if we read the documentation of the Millis function will see that that function returns an unsigned long so the moral of the story is when you are saving values from the Millis function into a variable that variable should be an unsigned long now a quick note about these unsigned Long's that can really screw some stuff up when you're using Millis and I know personally from experience if you're going to be doing any math with a variable that is an unsigned variable then make sure that these two things are true and you should be good first any other variables that are going to be used to change that variable should also be an unsigned long so let's say you've got a variable called previous time and you're gonna subtract a value from it and that value isn't another variable say it's like current time well make sure that that other variable is also an unsigned long okay so that's one thing the second thing is if you're going to be using any raw numbers to do a calculation on an unsigned variable make sure that at least one of the numbers in that calculation has a U and L for matter at the end of it so for example if you're dividing previous time which is an unsigned variable by 100 or maybe you're like dividing it by a thousand then make sure that the numbers written as 100 ul or 1000 ul so raw numbers like these they're called integer constants and if you don't put that ul formatter on the end then the calculation can create some really unexpected results and I won't get into the details about the strange results that can happen and necessarily why it happens but basically it has to do with when those numbers roll over after they get to their maximum value in that ul4 matter at the end of that raw number specifies that that numbers should be treated as an unsigned along so let's do a quick review of what we talked about first we said hey what's a hardware clock and a hardware clock is an electrical circuit that generates a signal at a consistent frequency then we talked about a timer counter and we said that the Arduino has a built-in timer counter and the timer counter counts the number of ticks that the clock has made and it keeps a running tally of those ticks then we talked about how the Millis function can get that running tally for us we talked about using a variable to store the value returned from Millis and we also talked about calling the Millis function inside of a condition finally we talked about using an unsigned long data type to store the value returned from Millis and we also talked about some of the precautions when doing math with an unsigned long variable data type all right well hey I hope you found this lesson useful check out the challenges at the end and I hope you have a great week bye you
Info
Channel: Programming Electronics Academy
Views: 71,752
Rating: 4.9461884 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 arduino, millis(), millis() arduino, electronics for beginners, computer programming for beginners, electronics projects, electronics basics, electronics repair, arduino millis, millis tutorial, arduino millis multitasking, arduino multitasking
Id: qn8SP93L3iQ
Channel Id: undefined
Length: 12min 49sec (769 seconds)
Published: Fri Feb 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.