Thanks for the help! The cppm decode part in the code worked!
But I am still unable to control a servo using the Futuba transmitter. The decode part seems correct; Somehow, the 'pwmWrite' operation is not being performed. The code prints 'valid' in serial monitor, which indicates that cppm is being received. But, the servo doesnt move at all through the remote(Tx). That means, i guess, pwmWrite is not happening in the code. Cant understand why !
volatile unsigned short rc_old = 0; // modified within the cppm_decode() handler
volatile unsigned short rc_new = 0;
volatile int channel = 0;
volatile int rc[8]; //Size of this array depends on the number of channels of the transmitter.
volatile int looper=0;
volatile int rc_valid = 0;
HardwareTimer timer(3);
const int pwmOutPin_steering = 4; // PWM pin for servo
const int pwmOutPin_throttle = 3; // PWM pin for throttle
volatile int outputPWMsteering = 0;
volatile int outputPWMthrottle = 0;
HardwareTimer timer2(2);
volatile int check;
void setup()
{
pinMode(15, INPUT_PULLDOWN);
attachInterrupt(15, cppm_decode, RISING);
pinMode(pwmOutPin_steering, PWM);
pinMode(pwmOutPin_throttle, PWM);
pinMode(10,OUTPUT);
timer.setPrescaleFactor(72);
timer.setOverflow(20000);
timer2.pause();
timer2.setPeriod(20000); // in microseconds
timer2.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer2.setCompare(TIMER_CH1, 1); // Interrupt 1 count after each update
timer2.attachCompare1Interrupt(command_generation);
timer2.refresh();
timer2.resume();
HardwareTimer timer2(2);
volatile int check;
}
void loop(){
//CONTROL LOOP: Control Decisions are taken in every Twenty Milliseconds
if(check)
{
//SerialUSB.println("Timer Overflow");
if(rc_valid)
{
SerialUSB.println("valid");
outputPWMsteering = rc[1];
outputPWMthrottle = rc[3];
pwmWrite(pwmOutPin_steering, outputPWMsteering);
pwmWrite(pwmOutPin_throttle, outputPWMthrottle);
//pwmWrite(4, rc[1]);
//pwmWrite(3, rc[3]);
delay(1000);
}
check = 0;
}
}
void command_generation(){
check = 1; //Whenever the 20ms timer overflows, the flag 'check' is raised.
}
//Interrupt function to decode CPPM
void cppm_decode() {
if(channel==0)
{
timer.setCount(0);
rc_old = 0;
}
rc_old = rc_new;
rc_new = timer.getCount();
if((rc_new-rc_old) < 600)
rc_valid = 0; //Indicates the Tx is OFF.
if((rc_new-rc_old) > 2500)
{
timer.setCount(0);
channel = 1;
rc_old = 0;
rc_new = 0;
rc_valid = 1; //Indicates the Tx is ON.
}
else
{
rc[channel] = rc_new-rc_old;
channel = (channel + 1)%7 ;
}
}