Hi All!
I have been working on an autopilot using the arduino since the start of the summer. In order for this project to work correctly, it is essential that I can switch between radio control and arduino (autopilot) control. In a earlier attempt, I used the arduino to throw some 5v relays so the servos signal wire would switch between the radio receiver and the arduino. In an effort to reduce power consumption/simplify the circuit, I am trying to read the receiver directly, and then the arduino gets to choose if it passes the signal through to the servos or if it should be on autopilot.
So when I upload my code to my arduino, the code works perfectly, it as if the receiver was controlling the servos by itself. However, when I switch to battery power, there is no response, even though the arduino is powered. It as if the i need to upload code every time for the arduino to work correctly.
my code comes from the website rcarduino. i stole most of mine from RCArduino: How To Read Multiple RC Channels
I do not have a picture of my circuit right now but I can easily describe it: the servos still receive power from the receiver. The signal wire from the receiver is sent to an arduino input. Another arduino output is then connected to the servo signal input. I have tried powering both the arduino and receiver seperately, and together (ideally i would like to have one battery at the end). So I have 11.1V lipo battery going to the arduino's DC jack, and then the 5v out from the arduino goes to the receiver.
sorry, maybe i can post a picture later
any suggestions would be appreciated!!
here is a copy of the code i am running:
#include <Servo.h>
#include <PinChangeInt.h>
//Declare Receiver Inputs to read channel signals
#define ESC_IN 8
#define ELEVATOR_IN 13
#define AILERON_IN 11
//Declare bit-flags to indicate if which channels have new signals
#define ESC_FLAG 1
#define ELEVATOR_FLAG 2
#define AILERON_FLAG 4
//Holds update flags defined above
volatile uint8_t bUpdateFlagsShared;
//Shared variables are updated by the ISR
volatile uint16_t ESC_IN_SHARED, ELEVATOR_IN_SHARED, AILERON_IN_SHARED;
//These are used to to to record rising edge of a pulse
uint32_t ESC_START, ELEVATOR_START, AILERON_START;
//Declare servos
Servo esc, elevator, aileron;
void setup(){
Serial.begin(9600);
//Attach Servos
esc.attach(7);
elevator.attach(12);
aileron.attach(10);
//Attaches interrupts using PinChangeInt library
PCintPort::attachInterrupt(ESC_IN, escRead, CHANGE);
PCintPort::attachInterrupt(ELEVATOR_IN, elevatorRead, CHANGE);
PCintPort::attachInterrupt(AILERON_IN, aileronRead, CHANGE);
}
//void loop(){}
void loop(){
//local variables to hold channel inputs
static uint16_t ESC, AILERON, ELEVATOR;
//local updated flags
static uint8_t bUpdateFlags;
//check shared flags to see if we have a new signal
if(bUpdateFlagsShared){
noInterrupts();
bUpdateFlags = bUpdateFlagsShared;
if(bUpdateFlags & ESC_FLAG){
ESC = ESC_IN_SHARED;
}
if(bUpdateFlags & ELEVATOR_FLAG){
ELEVATOR = ELEVATOR_IN_SHARED;
}
if(bUpdateFlags & AILERON_FLAG){
AILERON = AILERON_IN_SHARED;
}
bUpdateFlagsShared = 0;
interrupts();
}
if(bUpdateFlags & ESC_FLAG){
esc.writeMicroseconds(ESC);
}
if(bUpdateFlags & ELEVATOR_FLAG){
elevator.writeMicroseconds(ELEVATOR);
}
if(bUpdateFlags & AILERON_FLAG){
aileron.writeMicroseconds(AILERON);
}
bUpdateFlags = 0;
}
void escRead(){
if(digitalRead(ESC_IN) == HIGH){
ESC_START = micros();
}else{
ESC_IN_SHARED = (uint16_t) (micros() - ESC_START);
bUpdateFlagsShared |= ESC_FLAG;
}
}
void elevatorRead(){
if(digitalRead(ELEVATOR_IN) == HIGH){
ELEVATOR_START = micros();
}else{
ELEVATOR_IN_SHARED = (uint16_t) (micros() - ELEVATOR_START);
bUpdateFlagsShared |= ELEVATOR_FLAG;
}
}
void aileronRead(){
if(digitalRead(AILERON_IN) == HIGH){
AILERON_START = micros();
}else{
AILERON_IN_SHARED = (uint16_t) (micros() - AILERON_START);
bUpdateFlagsShared |= AILERON_FLAG;
}
}