Periodic Operation

This library allows users to call a function periodically.
MsTimer2, one of types, is compatible with Arduino One described in Arduino Playground. It is necessary to specify #include <MsTimer2.h> for using MsTimer2.

MsTimer2::set

Description
Specifies a call back function executed in the specified interval timer interrupt handler.
Syntax
MsTimer2::set(unsigned long ms, void (*function)())
Parameters
ms: Interval(ms)
function: Function name
Returns
None
Notes
The handler is executed by disabled interrupt. In case of executing with use of interrupt, permit an interrupt by interrupt().

MsTimer2::start

Description
Start the timer after set
Syntax
MsTimer2::start()
Parameters
None
Returns
None

MsTimer2::stop

Description
Stop the timer
Syntax
MsTimer2::stop()
Parameters
None
Returns
None

Sample Program

This sample tries to use MsTimer2.


#include <Arduino.h>
#include <MsTimer2.h>
int ledpin = PIN_LED_GREEN;
void flash() {
  static boolean output = HIGH;
 
  digitalWrite(ledpin, output);
  output = !output;
}
 
void setup() {
  pinMode(ledpin, OUTPUT);
 
  MsTimer2::set(500, flash); // 500ms period
  MsTimer2::start();
}
 
void loop() {
}