This sketch configures a timer to count like a wall clock, and perform an event every twelve hours. (It's a little hard to imagine why we couldn't use an alarm for this; we'll pretend it really needs to be an extremely accurate twelve hours.)

We saw that a timer is specified by two parameters: Its 'tick speed' in microseconds per tick, and how many ticks before it 'laps' or returns to zero.

A regular clock, for example, might tick once per second, and lap after twelve hours -- or in terms the begin() method understands: Tick once per 1,000,000 microseconds, and lap after 60*60*12 ticks.

// Hardware Timer sketch 2: Print a message every twelve hours precisely.

// Even though interrupts are disabled and we need to be quick, we can
// do (a modest amount of) printing inside a hardware timer handler,
// because the print functions don't actually wait to perform the
// output themselves, they just pass the data to the core software to
// ship out when it can.
void myTimerHandler() {        
  println("another twelve hours");

void setup() {
  Timer1.begin(1000000,60*60*12); // One second tick, 12 hour lap 
  Timer1.setLapHandler(myTimerHandler);  // Register a per-lap handler
  Timer1.start();                        // And start the timer

void loop() { 
  /* Nothing to do */

Generated on Fri Apr 22 06:54:11 2011 for SFB by doxygen 1.5.9