Hi Guys, I'm having trouble with some of my code. Basically I am trying to create an Arduino Multiplexer. I have four PWM outputs (Speed Controller, Aileron Servo, Elevator Servo and Rudder Servo). I wish to control these outputs from two different sources, The Autopilot (system is in a remote control plane) I am building, and the normal Reciever input. Thus there will be two modes, Auto Mode, and Manual Mode. Auto Mode is ON when the transmitter is either off or the GEAR switch is on. Otherwise the ARDUINO reads the normal reciever inputs and outputs them to the servos.
The problem I am having is that the "pulseIn" command in arduino is too slow to read 4 signals which results in missed pulses. Here is my code:
int minPulse = 0; // Pulse for minimum servo position (microseconds)
int maxPulse = 2500; // Pulse for maximum servo position (microseconds)
int refreshTime = 20; // the time needed in between pulses (milliseconds)
int duration = 0;
int lastPulse1 = 0;
int lastPulse2 = 0;
int lastPulse3 = 0;
int lastPulse4 = 0;
int lastPulse5 = 0;
int lastPulse6 = 0;
int lastPulse7 = 0;
//reciever inputs
const int throttle_in_rec = 2;
const int aile_in_rec = 3;
const int elev_in_rec = 4;
const int rudder_in_rec = 5;
const int gear_in_rec = 6;
//autopilot inputs
const int throttle_in_auto = 7;
const int aile_in_auto = 8;
const int elev_in_auto = 9;
//const int rudder_in_auto = 10;
//Servo Outputs
const int throttle_out = 10;
const int aile_out = 11;
const int elev_out = 12;
const int rudder_out = 13;
void setup()
{
//Serial.begin(9600);
pinMode(gear_in_rec, INPUT);
pinMode(throttle_out, OUTPUT);
pinMode(aile_out, OUTPUT);
pinMode(elev_out, OUTPUT);
pinMode(rudder_out, OUTPUT);
}
//************************************************************************************************
void loop()
{
duration = pulseIn(gear_in_rec, HIGH, 50000); //Read Transmitter Switch Signal
if(duration < 1100 && duration > 1000)
auto_mode(); //Manually activated return, Input B
if(duration < 100)
auto_mode(); //Loss of signal, Input B
else if(duration < 2000 && duration > 1850)
manual_mode(); //Manual Mode, Input A
}
//************************************************************************************************
void manual_mode()
{
//Read Reciever Inputs
int throttle = pulseIn(throttle_in_rec,HIGH, 20000); //Read Reciever throttle channel
if(throttle > minPulse && throttle < maxPulse)
{ //If the value is valid
do_servo(throttle, throttle_out, lastPulse1); //Update Servo Position
lastPulse1 = millis();
}
int aileron = pulseIn(aile_in_rec,HIGH,20000);
if(aileron > minPulse && aileron < maxPulse)
{
do_servo(aileron, aile_out, lastPulse2);
lastPulse2 = millis();
}
int elevator = pulseIn(elev_in_rec,HIGH,20000);
if(elevator > minPulse && elevator < maxPulse)
{
do_servo(elevator, elev_out, lastPulse3);
lastPulse3 = millis();
}
int rudder = pulseIn(rudder_in_rec,HIGH,20000);
if(rudder > minPulse && rudder < maxPulse)
{
do_servo(rudder, rudder_out, lastPulse4);
lastPulse4 = millis();
}
}
//************************************************************************************************
void auto_mode()
{
//Read Reciever Inputs
int throttle = pulseIn(throttle_in_auto,HIGH, 20000); //Read Reciever throttle channel
if(throttle > minPulse && throttle < maxPulse) //If the value is valid
do_servo(throttle, throttle_out, lastPulse5); //Update Servo Position
int aileron = pulseIn(aile_in_auto,HIGH,20000);
if(aileron > minPulse && aileron < maxPulse)
do_servo(aileron, aile_out, lastPulse6);
int elevator = pulseIn(elev_in_auto,HIGH,20000);
if(elevator > minPulse && elevator < maxPulse)
do_servo(elevator, elev_out, lastPulse7);
}
//************************************************************************************************
void do_servo(int pulse, int servoPin, int lastPulse)
{
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime)
{
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}
Is there a way to get around this?
Cheers,
Will