How To Read Multiple RC Channels

How To Read Multiple RC Channels -  This post builds on previous posts to show a technique for reading multiple radio control receiver channels using an Arduino.
The Arduino Leonardo and Micro use the ATMega 32u4 chip which supports interrupts on fewer pins than the ATMega 328 used in Arduino UNOs and Minis. The pins used by the sketch have been rearranged so that the code can now be run the Leonardo and Micro as well.

If you have previously used the code, please ensure that you update your connecteds by swapping the input and output pins.

Reading RC Channel Signals
Reading a single RC channel is relatively simple and can be outlined as -
1) Attach a pin change interrupt to the signal pin.
2) Create an interrupt service routine which will be called whenever the signal pin changes from high to low or low to high.

In the interrupt service routine we need to -
1) Check if the signal pin is high or low
2) If its high, this is the rising edge of the pulse, record the time using micros()
3) If its low, this is the falling edge. We are interested in the pulse duration, so if we call micros() to get the current time and then subtract from it the time we recorded in 2) for the rising edge we get the pulse duration.

To attach the interrupt using the pin change interrupt library we call -
PCintPort::attachInterrupt(THROTTLE_IN_PIN, calcThrottle,CHANGE); 
The interrupt service routine can be written as follows -
void calcThrottle()
{
  // if the pin is high, its a rising edge of the signal pulse,
  // so lets record its value
  if(digitalRead(THROTTLE_IN_PIN) == HIGH)
  {
    ulThrottleStart = micros();
  }
  else
  {
    // else it must be a falling edge, so lets get the time and subtract the time
    // of the rising edge this gives use the time between the rising and falling
    // edges i.e. the pulse duration.
    unThrottleInShared = (uint16_t)(micros() - ulThrottleStart);
  }
}
So all we need to do is cut and paste the attachInterrupt and interrupt service routine code three times to read three channels ?
No. Its not that simple, but its really not very complicated either.
There are four considerations which apply when using interrupts -
1) Volatile Shared Variables
2) Variable Access
3) Fast Interrupt Service Routines
4) Timing

For more detail How To Read Multiple RC Channels : http://rcarduino.blogspot.com/2012/04/how-to-read-multiple-rc-channels-draft.html
Back To Top