timer1.cpp

This sketch uses a timer to blink the red LED every two seconds (precisely). It uses Timer1, but that could have been Timer2 or Timer3 just as easily.

The overall behavior of a timer is specified by two parameters: How fast it 'ticks', in terms of microseconds per tick, and how many ticks before it 'laps' or returns to zero.

By default, a timer ticks once every 1,000 microseconds -- which is a millisecond, one thousandth of a second, so by default a timer ticks 1,000 times in one second. Also by default, a timer laps after 1,000 ticks. So, 1/1,000 second per tick times 1,000 ticks per lap produces one second per lap, which is just what we want in this case.

// Hardware Timer sketch 1: Blink red every other second, with high
// accuracy.

// We use ledToggle in a timer handler function.  Note that hardware
// timer handler functions are called with interrupts disabled!  We
// must be careful what we do, and be quick about it!
void myTimerHandler() {        
  ledToggle(BODY_RGB_RED_PIN);
}

void setup() {
  Timer1.begin();        // The default parameters to begin() are fine
  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