HELP: 1 joystick 2 continuous servo motor

Hi,

I'm trying to use 1 joystick to control 2 continuous servo motor.
The servo motor I'm using is SRC SM-S4315R Continuous Rotation Servo

This is the code:

#include <Servo.h>

const int servo1 = 2; // first servo
const int servo2 = 3; // second servo
const int joyH = 0; // L/R Parallax Thumbstick
const int joyV = 1; // U/D Parallax Thumbstick

int servoVal = (90); // 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, 0, 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 ("----------------");
}

However, the servo keeps rotating in one direction(say clockwise) while the joystick is still at neutral position.
And for example the servo2. When i pushed the joystick to one side (left or right) it stops. but after a while continue again. If I push it to that same direction max. It paused and turn in the other direction and pause and return to turning in clockwise.

What I want to do is for the servo to be at a initial position when the system starts. And when the joystick moves (say the x direction) to the left, servo2 moves anticlockwise, when it moves to the right, servo2 moves clockwise. And joystick at neutral position=servo stops moving.

Please help thanks!!

I think you need to use the input from the ADC in a more complex fashion.

It gives a value from 0-1023 for the full travel of the joystick from left to right.

What ever value it produces when the joystick is at the centre must be mapped (not necessarily with the map() function) to the stationary setting for the motor. Let's assume the ADC produce 500 when the joystick is in the middle. You need a little slack so the system is not too sensitive, so perhaps the stationary position should be anywhere between 450 and 550.

Then the values 0 - 450 should make the motor move to the left (faster for smaller numbers) and the values from 550 - 1023 should make the motor move to the right (faster for larger numbers).

I hope that makes sense.

...R

Robin2 took the words out of my mouth. First thing to do is organise a dead zone in the middle.

Part of that issue is that not only does the joystick not necessarily report exactly the same value every time it centres, but there's no guarantee that your servo will be be stationary on exactly 90 degrees (1500us). I was reading about one the other day where there's a hole in the side to calibrate that: you send it 90 degrees and then turn the little screw so it is actually stationary. Does yours have that facility?

Even if it does, it makes sense to leave a bit of leeway in the middle.

JimboZA:
I was reading about one the other day where there's a hole in the side to calibrate that: you send it 90 degrees and then turn the little screw so it is actually stationary. Does yours have that facility?

The product page says "it offers easy access to the rest-point adjustment potentiometer." so, yes.

The below has some info on the servo. It has some built in dead band. Have you adjusted the servo pots for no movement with the joystick in the neutral position? Most important, how are you powering the servos? They need an external power supply.