 volatile uint16_t counter;
 uint16_t pulse;
 uint16_t last;
+boolean first = true;
 unsigned long timer; // the timer
 uint16_t INTERVAL = 1000; // the repeat interval
 

  TIFR1 = (1<<ICF1); // clear pending
  TIMSK1 = (1<<ICIE1); // and enable
  
	USBGamepad.begin(rcChannels);  

#ifdef DEBUG
  timer = millis(); // start timer
	Serial.begin(115200);
#endif

}

void loop(){
  if (!triggered)
    return;
  pulse = counter - last;
  last = counter;
  if(pulse > (NEWFRAME_PULSE_WIDTH * TIMER_COUNT_DIVIDER)) {
    chan = 0;  // new data frame detected, start again
    USBGamepad.write(rcChannels);
  }
  else {
    if(pulse > (MIN_PULSE_WIDTH * TIMER_COUNT_DIVIDER - THRESHOLD) 
      && pulse < (MAX_PULSE_WIDTH * TIMER_COUNT_DIVIDER + THRESHOLD) 
      && chan < RC_CHANNELS_COUNT) 
    {
      rcChannels[chan] = pulse / TIMER_COUNT_DIVIDER; //store detected value      
    }
    chan++; //no value detected within expected range, move to next channel
  }

uint16_t rcChannels[RC_CHANNELS_COUNT];
uint8_t chan = 0; // current channel

ISR(TIMER1_CAPT_vect) { //Timer1 ISR capture vector
  counter = ICR1; //grab Timer1 value
  triggered = true; //we have interrupt
}

void setup() {
  cli(); //disable global interrupts until we are ready to accept them
  pinMode(PPM_CAPTURE_PIN, INPUT_PULLUP); //set PPM capture pin with pullup enabled

  // Input Capture setup
  // ICNC1: =0 Disable Input Capture Noise Canceler to prevent delay in reading
