Periodic Operation

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

attachIntervalTimerHandler

Description
Specifies a call back function executed in the interval timer interrupt handler. The specified call back function is called every 1ms of the interval timer interrupt once it's attached.
Syntax
attachIntervalTimerHandler(void(*)(unsigned long) function)
Parameters
function: Name of the handler
Returns
None
Remark
The libraries below can be called in the handler.
pinMode(), digitalWrite(), digitalRead(), millis(), micros(), delayMicroseconds(), min(), max(), constrain(), map(), lowByte(), highByte(), bitRead(), bitWrite(), bitSet(), bitClear(), bit(), randomSeed(), random()
Note: pinMode(), digitalWrite() may use the pins that are used in function loop(), which may causes a mis-operation.

detachIntervalTimerHandler

Description
Turns off the call back function in the interval timer interrupt handler.
Syntax
detachIntervalTimerHandler()
Parameters
None
Returns
None

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

attachCyclicHandler

Description
Specifies a cyclic call back function. The specified call back function is called at the cycle specified by this function.
Syntax
attachCyclicHandler(unsigned char number, void(*)(unsigned long) function, unsigned int time)
Parameters
number: ID (0 to 7)
function: Function of the interval timer interrupt
time: Interval time in ms
Returns
None

detachCyclicHandler

Description
Turns off the interval call back function.
Syntax
detachCyclicHandler(unsigned char number)
Parameters
number: ID (0 to 7)
Returns
None
Note
The cyclic handler is executed in a loop.

Sample Program

This sample tries to use both the 1ms periodic function and MsTimer2.


#include <Arduino.h>
#include <MsTimer2.h>
void using_mstimer2() {
  static boolean output = HIGH;
 
  digitalWrite(PIN_LED0, output);
  output = !output;
}
 
// this function is called every 1ms.
void using_intervaltimer(unsigned long ms){
  static boolean output = HIGH;
  static unsigned long time;
 
  if((ms - time) > 1000){
      interrupts(); // to allow USB operation
      Serial.println(ms);
      time = ms;
      digitalWrite(PIN_LED1, output);
      output = !output;
  }
}
 
void setup() {
    pinMode(PIN_LED0, OUTPUT);
    pinMode(PIN_LED1, OUTPUT);
    Serial.begin(9600);
 
  MsTimer2::set(500, using_mstimer2); // 500ms period
  MsTimer2::start();
 
  attachIntervalTimerHandler(using_intervaltimer);
}
 
void loop() {
}