Programming on using 2 XY joysticks to control 4 servo motors using arduino uno

can someone help me? i have problem controlling this 4 servos(SM-S4303R) with the 2 joysticks.

First test, I have tried to connect the servos to the arduino board by inserting the wire individually when the supply is on. I was able to control 4 servos with 2 joysticks perfectly.

Second test, when the wires is connected properly before the supply goes in, the servos unable to control.

The problem is Do i need to add more function into the codes or during the connection,the power needs to be on?

Below is the code:

#include <Servo.h>

Servo tilt_1, pan_1, tilt_2, pan_2;

int joyX1 = A1; // X - axis of Joystick1
int joyY1 = A0; // y - axis of Joystick1
int joyX2 = A3; // X - axis of Joystick2
int joyY2 = A2; // y - axis of Joystick2
int x1, y1, x2, y2;

void setup()
{
tilt_1.attach(6); // TILT on PIN 6
pan_1.attach(5); // PAN on PIN 5
tilt_2.attach(11); // TILT on PIN 11
pan_2.attach(10); // PAN on PIN 10
}

void loop()
{
x1 = joyX1; // y value btwn 0 and 1023)
y1 = joyY1; // x value btwn 0 and 1023)
x1 = map(analogRead(joyX1), 0, 1023, 900, 2100);
y1 = map(analogRead(joyY1), 0, 1023, 900, 2100);
tilt_1.write(x1);
pan_1.write(y1);
x2 = joyX2; // y value btwn 0 and 1023)
y2 = joyY2; // x value btwn 0 and 1023)
x2 = map(analogRead(joyX2), 0, 1023, 900, 2100);
y2 = map(analogRead(joyY2), 0, 1023, 900, 2100);
tilt_2.write(x2);
pan_2.write(y2);
delay(15);
}

Really appreciate your feedback. thanks

Do not attempt to power 4 servos directly from the Arduino 5V and GND pins. They will take too much current. Use an external power supply. Connect the servo positive lead to the external supply positive, its negative lead to the external supply GND, its signal lead to the Arduino and Arduino GND to the external supply GND.

I suspect that when you power up the Arduino with the servos connected the initial current required by the 4 servos is too much and the polyfuse on the Arduino opens thus stopping the Arduino running. Inserting the servo wires one at a time reduces the initial current but running the servos could cause an output pin to fail due to the current taken, hence the advice to use an external power supply.

Now, about your code

  x1 = joyX1;    // y value btwn 0 and 1023)
  y1 = joyY1;    // x value btwn 0 and 1023)
...
  x2 = joyX2;    // y value btwn 0 and 1023)
  y2 = joyY2;    // x value btwn 0 and 1023)

What are these lines lines for ? The joyX* values will never change in you code and why copy them anyway ?

Yeo:
Meaning to say that is a redundant line for doing it?

Just simply:
joyX1 = map(analogRead(joyX1), 0, 1023, 900, 2100);
joyY1 = map(analogRead(joyY1), 0, 1023, 900, 2100);
tilt_1.write(joyX1);
pan_1.write(joyX2);

Sorry No....
You need this..

 x1 = map(analogRead(joyX1), 0, 1023, 900, 2100); 
 y1 = map(analogRead(joyY1), 0, 1023, 900, 2100);
 tilt_1.write(x1); 
 pan_1.write(y1);

You need to read the analog inputs.
Tom.. :slight_smile:

i want the servo to hold on that position until second signal is given

It is NOT at all clear what you are talking about. If you have a defined relationship between the joystick position and the servo position, letting go of the joystick, letting it return to center, is, obviously, going to move the servo too.

On the other hand, you talk about continuous servos, and some cryptic piece of text, which google says is a Polulo continuous rotation servo, which means that you can control its speed, NOT its position.

So, talking about the joystick controlling the servos position is just so much noise.

Try again.

Why did you start ANOTHER thread, on the same problem?

There's code to do what you want to do in #12 of this thread.

Moderator: merged duplicate threads

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom... :slight_smile: