Posted by Shantanu Sharma
I hope you are already aware of the basics of the RC communication and Hardware connection with the Arduino. If you are not, then just have a quick look at the previous blog in the series… 🙂
After looking into the problems of the polling method, The second method to interface the RC is Interrupt method. Well, as you know there are only two external hardware interrupts available in Arduino boards like Uno, Dueminalove etc.. (Ofcourse, Due is an exception 😛 ). For further clarification on this topic, click for Interrupts. I am taking Arduino Uno as a typical example here.
For interfacing all the six channels of RC you need 4 more interrupts. What can we do. Obviously we have to go for the software interrupts.
Here comes the PinChangeInterrupt for the rescue…
What are Pin Change interrupts? The ATmega processor has two different kinds of interrupts: “external”, and “pin change”. There are only two external hardware interrupt pins, INT0 and INT1, and they are mapped to Arduino pins 2 and 3. These interrupts can be set to trigger on RISING or FALLING signal edges, or on low level. Being hardware interrupts, they are very fast.
On the other hand the pin change interrupts can be enabled on any or all of the Arduino’s signal pins (Uno/Duemilanove/Nano). They are triggered equally on RISING or FALLING signal edges, so it is up to the interrupt code to set the proper pins to receive interrupts, to determine what happened (which pin? …did the signal rise, or fall?), and to handle it properly. Furthermore, the pin change interrupts are grouped into 3 “port”s on the MCU, so there are only 3 interrupt vectors (subroutines) for the entire body of 20 pins. This makes the job of resolving the action on a single interrupt even more complicated. The interrupt routine should be fast, but more the complication, lesser is the speed. The PinChangeInt library is designed to handle the Arduino’s pin change interrupts as quickly and reasonably as possible.
Well, how can you use it?? This is just a little finger’s work (literally :P)
1. Download the library from this link.
2. Extract the zip to the Arduino library folder on your computer.
3. Include the header file in the sketchbook.
4. Use it as normal interrupt function.
I used pinchangeint-v1.72, which contained 3 folders: PcChangeInt, cppfix,MemoryFree.
The skeleton sketchbook works like this after the inclusion of PcIntChange library.
Now the RC interfacing part…
We need to define the pinchange Interrupt for all the 6 pins to which channels are attached.
This is the code to get the value of the 1 channel to help you understand the code flow.. Read the rest of this entry →