Skip to content
Arduino - millis function - This function is used to return the number of milliseconds at the time, the Arduino board begins running the current program. The timer/counter counts the number of ticks that the clock has made and it keeps a running tally of those ticks.We talked about how the milli() function can get that running tally for us, and how we store that value in a variable. It means it’s generating a consistent signal, we can call this single a “tick” as in the “tick-tock” of old school analog clocks.So if we count how many “ticks” have occurred since the sketch has been running, we know how much time has passed since it was powered up.Thankfully the Arduino already has a module that counts these “ticks” for us, and it’s called a “timer/counter module” … go figure!This module can do lots of things, but for our purposes, let’s just examine how it can:So the timer/counter starts counting “clock ticks” as soon as the Arduino powers up. Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). This number overflows i.e. MAKE SURE that other variable, currentTime, is also a The second thing is if you’re going to be using any raw numbers to do a calculation on an So for example, if you’re dividing previousTime, which is an Raw numbers like these are called integer constants and if you don’t put that UL formatter on the end, then the calculation can create some really unexpected results.We 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.The UL formatter at the end of that raw number tells the Arduino that it should be treated as an We covered what a hardware clock is an electrical circuit that generates a signal at a consistent frequency.We also talked about a timer/counter and we said that the Arduino has built in timer/counter. By submitting this form you agree to the Before we can explain this, we need some context. 2. So much so that if we look at the Arduino reference literature, we see the To summarize, when saving values form the millis function, save them into variables of the datatype First, any other variables that are going to be used to change that variable should also be an Let’s say you’ve got a variable called previousTime and you’re going to subtract a value from it, and that value is in another variable, say it’s like currentTime. After 1 minute of being powered up, the variable will be equal to 60,000 (60 secs x 1000 ms).Another way to get the value of millis() is to call the function inside of a condition. The Arduino Reference text is licensed under a Data type: This example code prints on the serial port the number of milliseconds passed since the Arduino board started running the code itself.Please note that the return value for millis() is of type What the tensile strength of a rubber band is Did you know that it gives you access to the Arduino internal timer counter hardware which can be used for the timing of different events?We will discuss this and more in the video tutorial below.What is this millis() function anyway? You are the best tutor I’ve seen. Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647. This number will overflow (go back to zero), after approximately 50 days. Enter the millis function.To put it simply, the millis() function gives us access to the running tally that the timer/counter has been keeping track of. Unlike standard longs unsigned longs won’t store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1). Since millis will never return a negative value, we can use an This is perfect for the millis function. ... [Data Types] Description. Doubts on how to use Github? Since previous time will be smaller than current time it’ll produce a negative result if you use the example above if(previousTime – currentTime > 10000UL ) so shouldn’t it be if(currentTime-previousTime > 10000UL ) ?Thanks for pointing that Clive – you are absolutely correct!