I have the circuit down so far. I am currently stuck on how to drive the 2 motors and servo without them interfering with each other. I tried 2 layouts:
-
Hook the servo directly to the receiver (since nothing needs to be changed about this servo for the blimp). Whenever the throttle for the motors are on, the servo goes to a certain position and just gets stuck there. If the throttle is off, the servo returns to the neutral position. The servo jitters a lot.
-
Have the Arduino read in the PPM from the receiver and use that as the microseconds parameter in the myservo.writeMicroseconds() function. The same thing happens as before.
I currently have 3 pulseIn's in my code which I know will obviously not work. I have taken a look a bunch of interrupt examples, tried some of them, but am not sure really how to implement them in my code. Can anyone show how an interrupt can be used to drive 2 motors and a servo using PWM?
Please note in the code that I don't have differential steering implemented yet. I'll be sticking with the simple, left motor or right motor on/off to steer (aka no throttle mixing).
#include <Servo.h>
Servo servoCh2;
int pinCh1 = 2; //input pin for steering left/right
int pinCh2 = 3; //input pin for steering up/down
int pinCh3 = 4; //input pin for throttle
int pinMotor1 = 5; //output pin for left motor
int pinMotor2 = 6; //output pin for right motor
int pinServoCh2 = 7; //output pin for servo steering up/down
//const int MIN_CH2 = 970;
//const int MAX_CH2 = 1800;
const int MIN_CH3 = 1090; //minimum PPM value read for throttle stick
const int MAX_CH3 = 1800; //maximum PPM value read for throttle stick
unsigned long durationCh1; //PPM value read in for Channel 1
unsigned long durationCh2;
unsigned long durationCh3;
void setup() {
servoCh2.attach(pinServoCh2);
pinMode(pinMotor1, OUTPUT);
pinMode(pinMotor2, OUTPUT);
Serial.begin(9600);
}
void loop() {
durationCh1 = pulseIn(pinCh1, HIGH, 50000);
durationCh2 = pulseIn(pinCh2, HIGH, 50000);
servoCh2.writeMicroseconds(durationCh2);
durationCh3 = pulseIn(pinCh3, HIGH, 50000);
if(durationCh3 < MIN_CH3) { //based off readings which are set as min/max constants
durationCh3 = MIN_CH3; //any value below or above will be ignored in order to be
} else if(durationCh3 > MAX_CH3) { //mapped correctly
durationCh3 = MAX_CH3;
}
//durationCh1 = map(durationCh1, 1090, 1800, 0, 255);
durationCh3 = map(durationCh3, MIN_CH3, MAX_CH3, 0, 255); //map PPM to a scale from 0-255
//durationCh1 = pulseIn(pinCh1, HIGH);
//durationCh3 = pulseIn(pinCh3, HIGH);
analogWrite(pinMotor1, durationCh3);
analogWrite(pinMotor2, durationCh3);
Serial.print("Ch1: "); //for debugging
Serial.println(durationCh1);
Serial.print("Ch2: ");
Serial.println(durationCh2);
Serial.print("Ch3: ");
Serial.println(durationCh3);
Serial.println();
}