skip navigational linksPJRC
Shopping Cart Download Website
Home Products Teensy Blog Forum
You are here: Teensy Teensyduino Libraries Metro

PJRC Store
Teensy 4.1, $31.50
Teensy 4.0, $23.80
Teensy
Main Page
Hardware
Getting Started
Tutorial
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: Included with the Teensyduino Installer

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.