I just got my first arduino uno 2 weeks ago and have been loving the learning process and think im doing amazing on it by being able to control 2 rc brushless motors using the 2 axis joystick that i was able to harvest from a wii controller:) . I am able to run both the motors forward and reverse but am struggling with the differential thrust (decreasing power on one side and adding power on the other) depending on the horizontal axis (yAxis). Any help would be appreciated and i have done trial and error to get this far and after i get done with this project i promise to learn the correct way..lol
Heres what i got so far, im suspecting its the "if" statements or stating right and leftThrust too many times but hopefully you all can shed some light on this. Thanks!
#include <Servo.h>
Servo LeftESC, RightESC;
int yAxis=A0;
int xAxis=A1;
int leftThrust;
int rightThrust;
void setup() {
LeftESC.attach(9, 1000, 2000); // attached to pin 9 I just do this with 1 Servo
Serial.begin(9600); // start serial at 9600 baud
LeftESC.write(2000);
delay(2000);
LeftESC.write(1000);
RightESC.attach(10,1000,2000); // attached to pin 10 I just do this with 1 Servo
Serial.begin(9600); // start serial at 9600 baud
RightESC.write(2000);
delay(2000);
RightESC.write(1000);
}
void loop() {
int yAxis = analogRead(A0);
int xAxis = analogRead(A1);
Serial.print("\ leftThrust: ");
Serial.print(leftThrust);
Serial.print("\ rightThrust: ");
Serial.print(rightThrust);
Serial.print("\ yAxis: ");
Serial.print(yAxis);
Serial.print("\ xAxis: ");
Serial.println(xAxis);
while (Serial.available() >0);
if (xAxis < 470)
// Turning Right
{
// Map the number to a value of 255 maximum
//xAxis = map(xAxis, 0, 470, 0, 128);
rightThrust = (yAxis - xAxis);
leftThrust = (yAxis + xAxis);{
// Don't exceed range of 0-255 for motor speeds
if(rightThrust <= 0)rightThrust = 128;
else if(leftThrust >= 255)leftThrust = 255;}
leftThrust = map(leftThrust, 0, 255, 1000, 2000);
rightThrust = map(rightThrust, 0, 255, 1000, 2000);
LeftESC.write(leftThrust);
RightESC.write(rightThrust);
}
else if (xAxis>550)
// Turning Left
{
//xAxis = map(xAxis, 550, 1023, 128, 255);
leftThrust = (xAxis - xAxis);
rightThrust = (yAxis + xAxis);{
// Don't exceed range of 0-255 for motor speeds
if (leftThrust >= 255 )leftThrust = 255;
else if(rightThrust <= 0)rightThrust = 128;}
leftThrust = map(leftThrust, 0, 255, 1000, 2000);
rightThrust = map(rightThrust, 0, 255, 1000, 2000);
LeftESC.write(leftThrust);
RightESC.write(rightThrust);
}
rightThrust = map(yAxis, 0,1020, 1000, 2000);
leftThrust = map(yAxis, 0,1020, 1000, 2000);
LeftESC.write(leftThrust);
RightESC.write(rightThrust);
delay(500);
}