Reference Language (extended) | Libraries | Comparison | Board
Returns the number of milliseconds since the Arduino board began running the current program.
None
The number of milliseconds since the current program started running, as an unsigned long. This number will overflow (go back to zero), after approximately 9 hours and 32 minutes.
long time; void setup(){ Serial.begin(9600); } void loop(){ Serial.print("Time: "); time = millis(); //prints time since program started Serial.println(time); // wait a second so as not to send massive amounts of data delay(1000); } /* Frequency Test * Paul Badger 2007 * Program to empirically determine the time delay to generate the * proper frequency for a an Infrared (IR) Remote Control Receiver module * These modules typically require 36 - 52 khz communication frequency * depending on specific device. */ int tdelay; unsigned long i, hz; unsigned long time; int outPin = 11; void setup(){ pinMode(outPin, OUTPUT); Serial.begin(9600); } void loop() { for (tdelay = 1; tdelay < 12; tdelay++){ // scan across a range of time delays to find the right frequency time = millis(); // get start time of inner loop for (i = 0; i < 100000; i++){ // time 100,000 cycles through the loop digitalWrite(outPin, HIGH); delayMicroseconds(tdelay); digitalWrite(outPin, LOW); delayMicroseconds(tdelay); } time = millis() - time; // compute time through inner loop in milliseconds hz = (1 /((float)time / 100000000.0)); // divide by 100,000 cycles and 1000 milliseconds per second // to determine period, then take inverse to convert to hertz Serial.print(tdelay, DEC); Serial.print(" "); Serial.println(hz, DEC); } }
Note that the parameter for millis is an unsigned long, errors may be generated if a programmer, tries to do math with other datatypes such as ints.
int startTime; // should be "unsigned long startTime;" // ... startTime = millis(); // datatype not large enough to hold data, will generate errors
Corrections, suggestions, and new documentation should be posted to the Forum.
The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.