Hey all,
I am trying to increase the throw of the servo that I can get using an independent Rx/Tx transmitter receiver set. This is the particular set: http://rotor.co.in/show-detail.asp?prodid={A1268AF3-5423-4B08-BFB7-E97DCD4A22B4}
I am using an Arduino Mega and the code that I am using is this:
#include <Servo.h>
#define throttlepin 21
#define throttlepinDIGITAL 21
#define motor1pin 4
#define motor2pin 5
#define motor3pin 6
#define motor4pin 7
#define motor5pin 8
#define motor6pin 9
#define servo1pin 2
#define servo2pin 3
#define newthrottle 1
#define newservo1 2
#define newservo2 4
volatile unsigned long starttime=0;
volatile unsigned long s1starttime=0;
volatile unsigned long s2starttime=0;
volatile int throttleinput, servo1input, servo2input;
volatile uint8_t check;
volatile int servo1val, servo2val;
Servo motor1;
Servo motor2;
Servo motor3;
Servo motor4;
Servo motor5;
Servo motor6;
void setup() {
attachInterrupt(0, calcInput1Servo, CHANGE);
attachInterrupt(1, calcInput2Servo, CHANGE);
attachInterrupt(2, calcThrottle, CHANGE);
motor1.attach (motor1pin);
motor2.attach(motor2pin);
motor3.attach(motor3pin);
motor4.attach(motor4pin);
motor5.attach(motor5pin);
motor6.attach(motor6pin);
Serial.begin(4800);
}
void loop() {
noInterrupts();
//Servo 2 Loop
if (check & newservo2)
{
Serial.print("servo_main");
Serial.println(servo2input);
if (servo2input>1700)
{
servo2val= servo2input;
servo2val = map(servo2val, 1700,2200, 0, 180);
motor3.write(servo2val);
Serial.print("servo1: ");
Serial.println (servo2val);
}
else if (servo2input<1690)
{
servo2val= servo2input;
servo2val = map(servo2val, 1200,1680, 0, 180);
motor4.write(servo2val);
//Serial.print("servo2: ");
//Serial.println (servo2val);
}
//check|=newservo2;
//interrupts();
}
if (check & newthrottle)
{
//Serial.print("newthrottle: ");
//Serial.println (throttleinput);
motor5.write(throttleinput);
motor6.write(throttleinput);
}
//Servo 1 Loop
if (check & newservo1)
{
// Serial.print("servoi_main");
//Serial.println(servo1input);
if (servo1input>1490)
{
servo1val= servo1input;
servo1val = map(servo1val, 1490,1892, 0, 180);
motor1.write(servo1val);
//Serial.print("servoi1: ");
//Serial.println (servo1val);
}
else if (servo1input<1460)
{
servo1val= servo1input;
servo1val = map(servo1val, 1064,1460, 180, 0);
motor2.write(servo1val);
//Serial.print("servoi2: ");
//Serial.println (servo1val);
}
}
check=0;
interrupts();
}
void calcInput1Servo(){
noInterrupts();
if (digitalRead(servo1pin)==HIGH) {
s1starttime=micros();
}
else
if(s1starttime)
{
servo1input= (int) (micros()-s1starttime);
check|=newservo1;
s1starttime=0;
}
interrupts();
}
void calcThrottle(){
noInterrupts();
if (digitalRead(throttlepin)==HIGH) {
starttime=micros();
}
else
if(starttime)
{
throttleinput= (int) (micros()-starttime);
check|=newthrottle;
starttime=0;
}
interrupts();
}
void calcInput2Servo()
{
noInterrupts();
if (digitalRead(servo2pin)==HIGH)
{
s2starttime=micros();
}
else {
servo2input= (int) (micros()-s2starttime);
check|=newservo2;
}
interrupts();
}
I followed the guide given here: RCArduino: How To Read Multiple RC Channels
I used the general concept behind the code given on this website to suit my needs. What i am actually trying to do is to pass the Rx signals through the Arduino and then given them to the servo so that i can get 180 degrees motion of the servo using only half deflection of the analog stick of the transmitter.
I presume there is something wrong with the code or is there something wrong with the concept behind the code.
Please help as I am in dire need of this.
Thanks
Summit