                 REPEAT
         } state = IDLE;
 
+        unsigned int elapsed = timer1();
+
         switch (state) {
         case IDLE:
-                TCNT1 = 0;
                 state = HEAD_HIGH;
                 break;
         case HEAD_HIGH:
-                if (TCNT1 >= MS(8.9) && TCNT1 <= MS(9.1)) {
-                        TCNT1 = 0;
-                        state = HEAD_LOW;
-                } else {
-                        state = IDLE;
-                }
+                state = elapsed >= MS(8.9) && elapsed <= MS(9.1) ? HEAD_LOW : IDLE;
                 break;
         case HEAD_LOW:
-                if (TCNT1 >= MS(4.4) && TCNT1 <= MS(4.6)) {
+                if (elapsed >= MS(4.4) && elapsed <= MS(4.6)) {
                         state = DATA_HIGH;
                         bits = 0;
                         data[0] = data[1] = data[2] = data[3] = 0;
-                } else if (TCNT1 >= MS(2.15) && TCNT1 <= MS(2.35)) {
+                } else if (elapsed >= MS(2.15) && elapsed <= MS(2.35)) {
                         state = REPEAT;
                 } else {
                         state = IDLE;

        TCCR1C = 0;
        TCNT1 = 0;
        TIMSK1 = 0;
}

void loop() {
        static int mode = MODE_STATIC, strobe_on = 0, flash = 0;
        static float smooth_hue = 0, brightness = 1, strobe = 0, fade = 0,
                        static_r = 0, static_g = 0, static_b = 0,
                        fade1_r = 0, fade1_g = 0, fade1_b = 0,
                        fade2_r = 0, fade2_g = 0, fade2_b = 0;

        int cmd = command;
        command = 0;

        switch (cmd) {
        case LIGHT_RED:
                static_r = 1;
                static_g = static_b = 0;
                mode = MODE_STATIC;
                break;
        case LIGHT_GREEN:
                static_r = static_b = 0;
                static_g = 1;
                mode = MODE_STATIC;
                break;
        case LIGHT_BLUE:
                static_r = static_g = 0;
                static_b = 1;
                mode = MODE_STATIC;
                break;
        case LIGHT_SMOOTH:
                mode = MODE_SMOOTH;
                break;
        case LIGHT_OFF:
                static_r = static_g = static_b = 0;
                mode = MODE_STATIC;
                break;
        case LIGHT_BRIGHTER:

                break;
        case LIGHT_FADE:
                mode = MODE_FADE;
                hsv2rgb(random(1e6)/1e6, 1, 1, &a_r, &a_g, &a_b);
                hsv2rgb(random(1e6)/1e6, 1, 1, &b_r, &b_g, &b_b);
                break;
        case 0:
                break;
        default:
                a_r = a_g = a_b = 1;
                mode = MODE_STATIC;
                break;
        }
