Thank you very much. That was good reading. We looked alot on the example code and we have some questions about it.
// First Example in a series of posts illustrating reading an RC Receiver with
// micro controller interrupts.
// Subsequent posts will provide enhancements required for real world operation
// in high speed applications with multiple inputs.
// Posts in the series will be titled - How To Read an RC Receiver With A Microcontroller
#define THROTTLE_SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt
#define THROTTLE_SIGNAL_IN_PIN 2 // INTERRUPT 0 = DIGITAL PIN 2 - use the PIN number in digitalRead
#define NEUTRAL_THROTTLE 1500 // this is the duration in microseconds of neutral throttle on an electric RC Car
volatile int nThrottleIn = NEUTRAL_THROTTLE; // volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long ulStartPeriod = 0; // set in the interrupt
volatile boolean bNewThrottleSignal = false; // set in the interrupt and read in the loop
// we could use nThrottleIn = 0 in loop instead of a separate variable, but using bNewThrottleSignal to indicate we have a new signal
// is clearer for this first example
// tell the Arduino we want the function calcInput to be called whenever INT0 (digital pin 2) changes from HIGH to LOW or LOW to HIGH
// catching these changes will allow us to calculate how long the input pulse is
// if a new throttle signal has been measured, lets print the value to serial, if not our code could carry on with some other processing
// set this back to false when we have finished
// with nThrottleIn, while true, calcInput will not update
bNewThrottleSignal = false;
// other processing ...
// if the pin is high, its the start of an interrupt
if(digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH)
// get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
// easy to understand and works very well
ulStartPeriod = micros();
// if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
// start time ulStartPeriod from the current time returned by micros()
if(ulStartPeriod && (bNewThrottleSignal == false))
nThrottleIn = (int)(micros() - ulStartPeriod);
ulStartPeriod = 0;
// tell loop we have a new signal on the throttle channel
// we will not update nThrottleIn until loop sets
// bNewThrottleSignal back to false
bNewThrottleSignal = true;
To be able to make this code work we have do ad our ouputs (esc). And also make the nThrottleIn (uS) into degrees for the esc to read (1ms = 0 degrees, 2 ms = 180 degrees)? Maybe some sort of maping:
map (nThrottleSignal, 1000, 2000, 0, 180)
Something like that? And then write it out to the esc?
And also we are wondering a bit why there is a "if" line inside the "else" within the void calcInput section. Whats the function with having a if-line there when there is no else-line with it so to say.
As we said, we are not good at this and appreciate every answer/help/pointers we can get from you guys.