# timer4.cpp

Although combining red, green, and blue light should theoretically produce white, on the IXM turning on all three of the RGB LEDs produces a rather pinkish-purplish white, because the red LED is relatively quite strong and the green LED relatively weak. This final example sketch uses split times to 'pulse width modulate' the RGB pins, toning down the red and blue to produce a more balanced white.

In addition, it demonstrates the getSketchIndex() method, which allows a split timer handler to identify which split time index is currently being handled. In this example, it allows a single handler to be used to turn off each LED after its allotted fraction of a lap.

Even though the LEDs are actually flickering madly, the resulting visual appearance is of a single steady color, because this timer is running at 100Hz per lap. All we see, with our slow eyes, is the 1Hz blinky period.

    // Hardware Timer sketch 4: A whiter shade of blinky
    //
    // (Note: For the RGB leds specifically, this could also be done, and
    // better, by using the PWM support.  But this timer+interrupt
    // approach can be used with _any_ pins, not just certain ones.)
    //
    // This sketch uses 100 ticks per lap, which implies only 100
    // different intensities per color can be displayed.  A shorter tick
    // with a larger lap would provide finer control, if desired.

    #define PINS 3
    const u32 pins[PINS] = { BODY_RGB_RED_PIN, BODY_RGB_GREEN_PIN, BODY_RGB_BLUE_PIN };
    const u32 off[PINS] =  { 15,               100,                45 };

    bool on = true;                 // Controls blink on vs blink off

    void allSet() { for (u32 i = 0; i < PINS; ++i) ledSet(pins[i],on); }
    void offOne() { ledOff(pins[Timer1.getSplitIndex()-1]); }

    void setup() {
      Timer1.begin(100, 100);       // 10KHz tick, 100Hz lap -> no flicker!
      Timer1.setLapHandler(allSet); // Reset RGBs at the lap
      for (u32 i = 0; i < PINS; ++i) // Put same handler on all splits, with..
        Timer1.setSplitHandler(i+1,off[i],offOne); // ..different split times
      Timer1.start();               // And start the timer
    }

    void loop() {
      on = !on;                     // Flip blink state
      delay(500);                   // Wait about a half second
    }