skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Teensyduino Libraries Metro

PJRC Store
Teensy 3.1, $19.80
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
USB Cable, $4.00
Teensy
Main Page
Teensy 3.1
Getting Started
How-To Tips
Code Library
Projects
Teensyduino
Reference

Metro Library

Metro, by Thomas Ouellet Fredericks, makes it easy to schedule events to occur at regular intervals. You can creates as many Metro objects as you need, each for its own interval, and check them in your loop() function.

Download: Metro.zip (Version 2.3.2)

No modifications were made for Teensy.

Hardware Requirements

None. Metro uses the built-in timer.

Basic Usage

Metro myname = Metro(milliseconds);

Create a Metro object that schedules every "milliseconds". You can create as many Metro objects as you need, each with its own name and separate interval.

myname.check()

Check if the interval has elapsed. Returns true if it has, false if it has not (yet).

myname.interval(milliseconds)

Change to a new interval setting.

myname.reset()

Reset the interval.

Example Program

The example program, available from File > Examples > Metro > serialInterval uses Metro to schedule reading the analog inputs 4 times per second.

// This example sends a Serial message every 250 milliseconds

#include <Metro.h> // Include the Metro library

Metro serialMetro = Metro(250);  // Instantiate an instance

void setup() {
  Serial.begin(115200); // Start the Serial communication
}

void loop() {

  if (serialMetro.check() == 1) { // check if the metro has passed it's interval .
  // Output all the analog readings seperated by a space character
    for (int i = 0; i < 6; i++ ) {
      Serial.print (analogRead( i) );
      Serial.print(32,BYTE);
    }
    // Terminate message with a linefeed and a carriage return
    Serial.print(13,BYTE);
    Serial.print(10,BYTE);
  }
}

Details

Detailed information can be found at the official Metro library page.