Read RC Channels from RC receiver from 1 to 8 channels.

2 Reading single RC channel on Pin 2 to 7 using Pin Change Interrupt and 16bit Timer1 with precaler.
Part 1

/*
Read a single RC Channel out of the receiver.
Wire Receiver Gnd (Black or Brown) to Arduino Gnd.
Wire receiver Signal (White or Yellow) to Pin 4 of ATMega328 based Arduino.
Pin 4 is set as Pin Change Interrupt PCINT20 in Pin Change Mask Register 2 PCMSK2.
No probleme if another Pin on the same port is set as Pin Change Interrupt (It should never be the case!) 
Use Timer1 in Normal Mode with Overflow Interrupt to Measure RC PWM signal.
Return result PulseLength in µs (Tipicaly 1000 to 2000µs).
On Futaba System PulseLength range from 2000µs (Low) to 1000µs (High)
Return a Fail Safe Value if no signal is detected (2000µs for Futaba System).
Fail Safe Value can be changed to suits other system or requirement.
Micro Controller Clock Frequency shall be according to used MCU.
Prescaler can be changed but 8 is best fit and gives +/-0.5µs resolution.
More over with Prescaler at 8 the timer overflow occurs at 32.768ms.
This gives a fast Fail Safe responce (all frame is 20ms i.e 50Hz).
NOTE on TIMERS
Timer0 is a 08bit timer: PWM Pin05 and Pin06, timer functions, like delay(), millis() and micros() uses timer0
Timer1 is a 16bit timer: PWM Pin08 and Pin09, Servo library uses timer1 on Arduino Uno (timer5 on Arduino Mega)
Timer2 is a 08bit timer: PWM Pin03 and Pin11, the tone() function uses timer2.
So if you need Servo.Lib to work properly:
-Avoid the use of Timer0, no good to touch delay(), millis() and micros() fonctions
-Use Timer2 but you loose Tone fonction (Tone was important to me so I used Timer1)
DON'T FORGET TO SET SERIAL MONITOR AT 115200 BAUD!
*/

#include <avr/io.h>
#include <avr/interrupt.h>                          //Needed to use interrupts
#include <stdint.h>                                 //Has to be added to use uint8_t...
#include <math.h>                                   //For using log() function

//MICRO CONTROLLER CLOCK FREQUENCY
#define MCU_CLOCKFREQ         16                    //MHz

//TIMER1 PRESCALER
#define TI1_PRESCALER         8                     // 1, 8, 64, 256 or 1024 for Timer1

//+/-0.5µs @ 16MHz with Prescaler = 8   Overflow every  32.768ms (bit more than 20ms (50Hz) of RC PWM frame)
//+/-4.0µs @ 16MHz with Prescaler = 64  Overflow every 262.144ms

//PIN INPUT FOR PIN CHANGE INTERRUPT (Caution! 0 is RXD/1 is TXD)
#define PI_CH_INT_PIN         4                     // 0, 1, 2, 3, 4, 5, 6 or 7 (Port D only for now)

//RADIO SYSTEM DEFAULT FAIL SAFE VALUE (ex Low Throttle)
#define FAIL_SAFE_VAL         2000                  //FUTABA=2000=LOW

//Timer Bitwise Operators Calculated BitShift Left  using Log Base 2
const int ShiftL = (log (TI2_PRESCALER/MCU_CLOCKFREQ) / log (2));
//Timer Bitwise Operators Calculated BitShift Right using Log Base 2
const int ShiftR = (log (MCU_CLOCKFREQ/TI2_PRESCALER) / log (2));
//Capture Flag
volatile boolean Flag = true;
//Variables holding timestamps
volatile uint16_t InTimeStamp;
volatile uint16_t UpTimeStamp;
volatile uint16_t DnTimeStamp;
//PulseLength
volatile uint32_t PulseLength;
//Port D Status and Record
volatile uint8_t StatusPortD;
volatile uint8_t RecordPortD;                       //= 0xFF (Set 11111111 Default is high because the pull-up: Useless?)
volatile uint8_t ChangedBits;

void setup() {
  delay(100);
  Serial.begin(115200);
  delay(100);
  Serial.println(" "); 
  delay(100);
  Serial.println("Start"); 
  delay(100);
  Serial.print("ShiftL=");
  Serial.println(ShiftL);
  Serial.print("ShiftR=");
  Serial.println(ShiftR);
  delay(100);
  //Timer1 (16bits) Setting and Starting
  SetStartTimer1();
  //Initialization Pin Change Interrupt
  InitPinChangeInt();
  delay(500);
}

void loop() {
  //HERE IS YOUR CODE
  //When Flag is True Read Pulse Length (available)
  if (Flag == true) Serial.println(PulseLength);
}

//Timer1 (16bits) Setting and Starting
void SetStartTimer1(void) {
  //Disable global interrupts
  cli();
  //Clean the registers
  TCCR1A = 0;      
  TCCR1B = 0;
  //Clear Pending Interrupts
  TIFR1|=(1<<TOV1);
  //Enable overflow interrupts
  TIMSK1|=(1<<TOIE1); 
  //Start timer with prescaller
  SetPrescaler(TI1_PRESCALER);
  //Enable global interrutps
  sei(); 
}

//Set Timer1 Prescaler
void SetPrescaler(int Prescaler) {
   //Select Case
  switch (Prescaler) {
    case 1:
      TCCR1B|=(1<<CS10);
      //Serial.println("Prescaler=1");
      break;
    case 8:
      TCCR1B|=(1<<CS11);
      //Serial.println("Prescaler=8");
      break;
    case 64:
      TCCR1B|=(1<<CS11)|(1<<CS10);
      //Serial.println("Prescaler=64");
      break;
    case 256:
      TCCR1B|=(1<<CS12);
      //Serial.println("Prescaler=256");
      break;
    case 1024:
      TCCR1B|=(1<<CS12)|(1<<CS10);
      //Serial.println("Prescaler=1024");
      break;
    default: 
      TCCR1B|=(1<<CS10);
      //Serial.println("Prescaler=1");
    break;  
  }  
}

See Next