Hello,
What I essentially did is used this person's idea (RCArduino: How To Read Multiple RC Channels), and a ATmega 328 to read 3 RC channels from a E-sky radio (The "information" pins are hooked up to Arduino pins 6,7,8), and pass thru this information to 3 Servos using the built-in servo library. The servos are on Arduino pins 2,3,4 and the servos are powered from a 9 volt battery.
Here's my the code with serial for debugging purposes:
#include <Servo.h>
#include <PinChangeInt.h>
const int ch1in = 6;
volatile unsigned long ch1sS = 0;
volatile unsigned long ch1eS = 1500;
volatile boolean fch1 = false;
unsigned long ch1s = 0;
unsigned long ch1e = 1500;
unsigned long ch1m = 1500;
const int ch2in = 7;
volatile unsigned long ch2sS = 0;
volatile unsigned long ch2eS = 1500;
volatile boolean fch2 = false;
unsigned long ch2s = 0;
unsigned long ch2e = 1500;
unsigned long ch2m = 1500;
const int ch3in = 8;
volatile unsigned long ch3sS = 0;
volatile unsigned long ch3eS = 1500;
volatile boolean fch3 = false;
unsigned long ch3s = 0;
unsigned long ch3e = 1500;
unsigned long ch3m = 1500;
Servo s1;
Servo s2;
Servo s3;
void setup () {
Serial.begin(9600);
PCintPort::attachInterrupt(ch1in, ch1, CHANGE);
PCintPort::attachInterrupt(ch2in, ch2, CHANGE);
PCintPort::attachInterrupt(ch3in, ch3, CHANGE);
s1.attach(2);
s2.attach(3);
s3.attach(4);
}
void loop (){
noInterrupts();
ch1e = ch1eS;
ch1s = ch1sS;
ch2e = ch2eS;
ch2s = ch2sS;
ch3e = ch3eS;
ch3s = ch3sS;
interrupts();
if (fch1 == true){
ch1m = ch1e - ch1s;
fch1 = false; }
if (fch2 == true){
ch2m = ch2e - ch2s;
fch2 = false; }
if (fch3 == true){
ch3m = ch3e - ch3s;
fch3 = false; }
Serial.print(ch1m);
Serial.write(' ');
Serial.print(ch2m);
Serial.write(' ');
Serial.println(ch3m);
s1.writeMicroseconds(ch1m);
s2.writeMicroseconds(ch2m);
s3.writeMicroseconds(ch3m);
}
void ch1(){
if (digitalRead(ch1in) == HIGH)
ch1sS = micros();
else{
ch1eS = micros();
fch1 = true;}}
void ch2(){
if (digitalRead(ch2in) == HIGH)
ch2sS = micros();
else{
ch2eS = micros();
fch2 = true;}}
void ch3(){
if (digitalRead(ch3in) == HIGH)
ch3sS = micros();
else{
ch3eS = micros();
fch3 = true;}}
Everything works perfectly (servo's work smoothly without jitter). But as soon as I remove the serial code as in the following sketch, the servos start jittering back and froth. :~ I tried adding delays of various amount but they didn't help. It appears that the serial influences the behaviour in some way, I don't understand. Maybe someone more experienced could have insight to why it happens so.
#include <Servo.h>
#include <PinChangeInt.h>
const int ch1in = 6;
const int ch2in = 7;
const int ch3in = 8;
volatile unsigned long ch1sS = 0;
volatile unsigned long ch1eS = 1500;
volatile boolean fch1 = false;
unsigned long ch1s = 0;
unsigned long ch1e = 1500;
unsigned long ch1m = 1500;
volatile unsigned long ch2sS = 0;
volatile unsigned long ch2eS = 1500;
volatile boolean fch2 = false;
unsigned long ch2s = 0;
unsigned long ch2e = 1500;
unsigned long ch2m = 1500;
volatile unsigned long ch3sS = 0;
volatile unsigned long ch3eS = 1500;
volatile boolean fch3 = false;
unsigned long ch3s = 0;
unsigned long ch3e = 1500;
unsigned long ch3m = 1500;
Servo s1;
Servo s2;
Servo s3;
void setup () {
PCintPort::attachInterrupt(ch1in, ch1, CHANGE);
PCintPort::attachInterrupt(ch2in, ch2, CHANGE);
PCintPort::attachInterrupt(ch3in, ch3, CHANGE);
s1.attach(2);
s2.attach(3);
s3.attach(4);
}
void loop (){
noInterrupts();
ch1e = ch1eS;
ch1s = ch1sS;
ch2e = ch2eS;
ch2s = ch2sS;
ch3e = ch3eS;
ch3s = ch3sS;
interrupts();
if (fch1 == true){
ch1m = ch1e - ch1s;
fch1 = false; }
if (fch2 == true){
ch2m = ch2e - ch2s;
fch2 = false; }
if (fch3 == true){
ch3m = ch3e - ch3s;
fch3 = false; }
s1.writeMicroseconds(ch1m);
s2.writeMicroseconds(ch2m);
s3.writeMicroseconds(ch3m);
delay(20); // I have tried various amounts of delay here, and no delay at all but none seam to help
}
void ch1(){
if (digitalRead(ch1in) == HIGH)
ch1sS = micros();
else{
ch1eS = micros();
fch1 = true;}}
void ch2(){
if (digitalRead(ch2in) == HIGH)
ch2sS = micros();
else{
ch2eS = micros();
fch2 = true;}}
void ch3(){
if (digitalRead(ch3in) == HIGH)
ch3sS = micros();
else{
ch3eS = micros();
fch3 = true;}}