Arduino - Millis

Arduino

Guide

Tutorials

Reference

Hardware

Software

Forum

Playground

Exhibition

Hacking

FAQ

Buy

Language | Libraries | Comparison

## unsigned long millis()

#### Description

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

#### Parameters

None

#### Returns

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.

#### Examples

    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);
      }
    }

#### See also

- delay
- delayMicroseconds

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

Edit Page | Page History | Printable View | Recent Site Changes