I am new to programming and Arduinos but I've decided to give a go for a project. I'm after a code that will read PWM signals from a RC receiver (Specifically aileron and throttle) and output this as 4 PWM signals to drive two H Bridges (Right motor forward/reverse and left motor forward/reverse).
This is my code that isn't working.
unsigned long timer[3];
byte last_channel[2];
int throttle;
int aileron;
void setup() {
pinMode(8, OUTPUT);//Right motor fowards
pinMode(9, OUTPUT);//Right motor reverse
pinMode(10, OUTPUT);//left motor fowards
pinMode(11, OUTPUT);//left motor reverse
PCICR |= (1 << PCIE0); //This enables pin change intterupt enabler
PCMSK0 |= (1 << PCINT0); // This tells the mask register
which pins will cause an intterupt (Pin 2)
PCMSK0 |= (1 << PCINT1); // (Pin 3)
Serial.begin(9600); //Sets the data rate in bits per second
}
void loop() {
}
ISR(PCINT0_vect) {
timer[0] = micros(); // registers intterupt time
//Throttle_Channel----------------------------------------------------------
if(last_channel[0] == 0 && PINB & B00000001 ) { // This checks if pin has
changed from low to high
last_channel[0] = 1;
timer[1] = timer[0];
}
else if(last_channel[0] == 1 && !(PINB & B00000001)){
last_channel[0] = 0;
throttle = timer[0] - timer[1]; //calculates the change in time and sets to
input[0]
//--------------------------------------------------------------------------
}
//Aileron_Channel-----------------------------------------------------------
if(last_channel[1] == 0 && PINB & B00000010 ) { // This checks if pin has
changed from low to high
last_channel[1] = 1;
timer[2] = timer[0];
}
else if(last_channel[0] == 1 && !(PINB & B00000010)){ //This checks if pin has
changed from high to low
last_channel[1] = 0;
aileron = timer[0] - timer[2]; //calculates the change in time and sets to i
input[0]
//--------------------------------------------------------------------------
}
//Calculations to put output in correct form--------------------------------
int throttle_pwm = (throttle - 1500) / 1000;
int aileron_pwm = (aileron - 1500) / 1000;
int v = (0.5 - abs(aileron_pwm)) * (throttle_pwm / 0.5) + throttle_pwm;
int w = (0.5 - abs(throttle_pwm)) * (aileron_pwm / 0.5) + aileron_pwm;
int lm = v + (w/2) * 255;
int rm = v - (w/2) * 255;
//--------------------------------------------------------------------------
if (lm > 0 && rm > 0){
analogWrite(8,rm);
analogWrite(9,0);
analogWrite(10,lm);
analogWrite(11,0);
}
else if (lm > 0 && rm <= 0){
analogWrite(8,0);
analogWrite(9,abs(rm));
analogWrite(10,lm);
analogWrite(11,0);
}
else if (lm <= 0 && rm > 0){
analogWrite(8,rm);
analogWrite(9,0);
analogWrite(10,0);
analogWrite(11,abs(lm));
}
else if (lm <= 0 && rm <= 0){
analogWrite(8,0);
analogWrite(9,abs(rm));
analogWrite(10,0);
analogWrite(11,abs(lm));
//--------------------------------------------------------------------------
}
}
It compiles fine but I've tried measure PWM at the four output and it doesn't work. If anyone could offer advice or guidance it would be greatly appreciated.