controlling ESCs with a joystick

Anything to do with programing the Arduino Platform.
Post Reply
A.R.E.S._R.O.V.
Posts: 20
Joined: Nov 23rd, 2015, 4:27 pm

controlling ESCs with a joystick

Post by A.R.E.S._R.O.V. »

If I have this code which controls two servos with a joystick how do I make it so that instead of controlling a servo motor, it controls a Forward/reverse ESC? I need it to control 4 ESCs. (motor one and two ccw when joypushed foreward, cw when back, and motor three and four are ccw when joy is pushed right, cw when left.)

pleeeeaaase help me. This is one of the last steps for my ROV and I've put so much work into it, I just want to see it swim!
:cry:


#include <Servo.h>

const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick

int servoVal; // variable to read the value from the analog pin

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo



void setup() {

// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo

// Inizialize Serial
Serial.begin(9600);
}


void loop(){

// Display Joystick values using the serial monitor
outputJoystick();

// Read the horizontal joystick value (value between 0 and 1023)
servoVal = analogRead(joyH);
servoVal = map(servoVal, 0, 1023, 0, 180); // scale it to use it with the servo (result between 0 and 180)

myservo2.write(servoVal); // sets the servo position according to the scaled value

// Read the horizontal joystick value (value between 0 and 1023)
servoVal = analogRead(joyV);
servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)

myservo1.write(servoVal); // sets the servo position according to the scaled value

delay(15); // waits for the servo to get there

}


/**
* Display joystick values
*/
void outputJoystick(){

Serial.print(analogRead(joyH));
Serial.print ("---");
Serial.print(analogRead(joyV));
Serial.println ("----------------");
}
User avatar
Oldsirhippy
Posts: 86
Joined: Oct 1st, 2013, 7:18 am

Re: controlling ESCs with a joystick

Post by Oldsirhippy »

@A.R.E.S._R.O.V. - ESCs are controlled with ppm (pulse position modulation) which is also how servos are controlled. So just connect the ppm control line for the servo to the ppm control line of the ESC.
A.R.E.S._R.O.V.
Posts: 20
Joined: Nov 23rd, 2015, 4:27 pm

Re: controlling ESCs with a joystick

Post by A.R.E.S._R.O.V. »

that's it?!? awesome, I will try when I get home...what about making the ESC go in reverse? I've seen that some ESC's will go in reverse from 0-90 degrees and forward from 90-180 deg. when sent commands from arduino, is that true?
User avatar
KR2_Diving
Posts: 391
Joined: Aug 30th, 2012, 11:43 am
Location: Currently: NW Suburbs of Chicago. Originally: NE Wisconsin

Re: controlling ESCs with a joystick

Post by KR2_Diving »

you got it!

you might need to force the value of 90 to be sent during start up because many ESCs require a neutral signal (90) at start up...
Post Reply