alarm2.cpp

This sketch demonstrates using alarms to create repeating events, rather than just one-shots. It uses three different alarms to flash the RGB LED's each at a different rate, from 1.234 seconds to 3.456 seconds.

To make a repeating event, one simply uses Alarms.set() to reset the alarm, from inside the alarm handler. Now, recall that to set an alarm, you need to know which alarm number you want to set. Well conveniently, when inside an alarm handler, you can call Alarms.currentAlarmNumber() to find out what alarm number caused this alarm handler to execute.

// Alarm sketch 2: Crazy lights - use alarms to blink lights at different rates

u32 redPeriod = 1234, greenPeriod = 2345, bluePeriod = 3456;

void redHandler(u32 when) {
  ledToggle(BODY_RGB_RED_PIN);
  Alarms.set(Alarms.currentAlarmNumber(), when+redPeriod);
}
void greenHandler(u32 when) {
  ledToggle(BODY_RGB_GREEN_PIN);
  Alarms.set(Alarms.currentAlarmNumber(), when+greenPeriod);
}
void blueHandler(u32 when) {
  ledToggle(BODY_RGB_BLUE_PIN);
  Alarms.set(Alarms.currentAlarmNumber(), when+bluePeriod);
}

void setup() {
  Alarms.set(Alarms.create(redHandler),0); // Create and set redHandler alarm
  Alarms.set(Alarms.create(greenHandler),0); // Ditto greenHandler
  Alarms.set(Alarms.create(blueHandler),0);  // Ditto blueHandler
}

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

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