# function of millis ()

Hi, I am newbie in arduino, so I still trying basic coding and still having project in push button and LED, but I got confuse in some case so I decided to search in internet, then that time I found the function of millis() but I couldnt figure it out what is the function of that and how do I use it.

It tells you the time. It's like looking at a clock on the wall. If you decide to do something 1000 milliseconds after something else, then record the time that you did the first thing and then look at the clock on the wall to see if the current time minus 1000 is greater than the time you recorded.

(Always use subtraction, it makes the maths easier for the computer.)

Well what don't you understand in the documentation???

Returns the number of milliseconds since the Arduino board began running the current program.

seems pretty self explanatory...

millis() returns the number of milliseconds since the last reset of the Arduino. Basically a form of current time.

Below a simple example that makes use of it.

``````void setup()
{
Serial.begin(115200);
}

void loop()
{
// last time that something was done
static unsigned long lastTime = 0;
// current time
unsigned long currentTime;

// get the current time
currentTime = millis();

// check if it's time to do something (every 5 seconds)
if(currentTime - lastTime >= 5000)
{
// do something
Serial.println("It's time to do something");

// update last time that something was done
lastTime = currentTime;
}
}
``````

The way I understand it, the millis() function is like a more advanced delay() function. The delay function pauses the program until the delayed time is up but you can't do anything with the program until the delay is over. The milli() function checks the time periodically but doesn't tie up the processor so you can still do things like move a servo motor, check the temperature, etc.

IF you look in the arduino Application under: FILE-->EXAMPLES-->DIGITAL-->BLINKWITHOUTDELAY

This is a good example program and the comments help tell you what the functions do.

freebird4446:
The way I understand it, the millis() function is like a more advanced delay() function.

That's not (quite) correct. It just gets the 'current time' that can be used to compare against a known 'time'; that way you can implement a non-blocking 'delay'.

Fortunejr:
I found the function of millis() but I couldnt figure it out what is the function of that and how do I use it.

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.

...R