Controlling 4 servos with 2 joysticks

Hello, I've been trying to make 4 servos move using 2 joysticks for my project, but i cant figure out how.
This is the code I've been using that works with 2 servos and 1 joystick

#include <Servo.h>
  
Servo servo1;
Servo servo2;
int joyX = 0;
int joyY = 1;
  
int servoVal;
  
void setup() 
{
  servo1.attach(3);
  servo2.attach(5);
}
  
void loop()
{
  
  servoVal = analogRead(joyX);
  servoVal = map(servoVal, 0, 1023, 0, 180);
  servo1.write(servoVal);

  servoVal = analogRead(joyY);
  servoVal = map(servoVal, 0, 1023, 70, 180);
  servo2.write(servoVal);
  delay(15);  
}

Is there a way to configure the code so 2 joysticks would control 4 servos? Or maybe remake the code so it would do that? (P.S. I use an external power supply for the servos)

Welcome to the forum

The simplest way to do what you want is to duplicate the code for the first joystick and 2 servos using different variables

That is not the smartest way to do it, but would work

1 Like

Try this code:

"Servo&Joy - Wokwi ESP32, STM32, Arduino Simulator

PS:
The code can still be improved.
Moving on to the servo.write function, but I don't know how to do :grinning: :grinning:

Yay, with the help of the web I managed to do it. :fireworks: :fireworks: :fireworks:

I managed to get it to work, heres a link to the simulation of the project if anyone has the same issue: Four servos controlled by Two joysticks - Wokwi ESP32, STM32, Arduino Simulator
Code:

#include <Servo.h>
  
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int joyX1 = 0;
int joyY1 = 1;
int joyX2 = 2;
int joyY2 = 3;
  
int servoVal1;
int servoVal2;
 
void setup() 
{
  servo1.attach(3);
  servo2.attach(5);
  servo3.attach(6);
  servo4.attach(9);

  servo1.write(0);
  servo2.write(0);
  servo3.write(0);
  servo4.write(0);
}
  
void loop()
{
  
  servoVal1 = analogRead(joyX1);
  servoVal1 = map(servoVal1, 0, 1023, 0, 180);
  servo1.write(servoVal1);

  servoVal1 = analogRead(joyY1);
  servoVal1 = map(servoVal1, 0, 1023, 70, 180);
  servo2.write(servoVal1);
  
  servoVal2 = analogRead(joyX2);
  servoVal2 = map(servoVal2, 0, 1023, 0, 180);
  servo3.write(servoVal2);

  servoVal2 = analogRead(joyY2);
  servoVal2 = map(servoVal2, 0, 1023, 70, 180);
  servo4.write(servoVal2);
  delay(15);  
}

nevermind, somewhy it only works in simulation, meanwhile in real life it only moves 2 servos with 1 joystick

Well, since it works in simulation, and it's dead simple, why not check your wiring?

already did, tried bunch of connection ways too, for example i tried connecting 2 servos to arduino 5v and gnd outputs and other 2 to external power supply. nothing seems to work (servos and joysticks are also tested and functioning as they supposed to)

When you use external power are you connecting a common GND between that power supply and the Arduino ?

yes

Are you using an Arduino Uno/Nano? Sorry... analog pins

Im using Arduino UNO

Sorry.... I was not thinking "analog pins"

Change your wiring (swap joysticks). Does that make a difference?

Tried that, output is still the same

Get a serial print of x1, y1, x2, y2. If values change but the servo does not, it could be an internal timing usage issue. If the values do not change, wiring is the issue.

Finally!! I managed to solve it! I think the issue was that I connected all servos to PWM pins, after I mixed it out a bit it got functional. Thanks to everyone that helped me. Here is the final code Im using:

#include <Servo.h>
  
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int joyX1 = 0;
int joyY1 = 1;
int joyX2 = 2;
int joyY2 = 3;

int servoVal1;
int servoVal2;

void setup() 
{
  servo1.attach(3);
  servo2.attach(5);
  servo3.attach(7);
  servo4.attach(8);

  servo1.write(0);
  servo2.write(0);
  servo3.write(0);
  servo4.write(0);

  Serial.begin(9600);
}

void loop()
{
  int x1 = analogRead(joyX1);
  servoVal1 = map(x1, 0, 1023, 0, 180);
  servo1.write(servoVal1);

  int y1 = analogRead(joyY1);
  servoVal1 = map(y1, 0, 1023, 70, 180);
  servo2.write(servoVal1);
  
  int x2 = analogRead(joyX2);
  servoVal2 = map(x2, 0, 1023, 0, 180);
  servo3.write(servoVal2);

  int y2 = analogRead(joyY2);
  servoVal2 = map(y2, 0, 1023, 70, 180);
  servo4.write(servoVal2);

  // Print joystick values to Serial Monitor
  Serial.print("x1: "); Serial.print(x1);
  Serial.print(", y1: "); Serial.print(y1);
  Serial.print(", x2: "); Serial.print(x2);
  Serial.print(", y2: "); Serial.println(y2);

  delay(15);  
}

Schematics:

Using PWM pins should make no difference.

The most likely cause of the problem was more likely to be bad wiring/bad connections. Moving the wiring to other pins has fixed that problem. Which pins you are using is incidental

Another thought. Are you really using a 9V PP3 battery to power the project as shown ?

Im not sure about bad wiring because right after i switched the pins where signal of servos goes it fixed, i didn't change anything on the breadboard. About the 9v battery, no im using a variable supply with roughly 5v being the output. But i tried it with 9v battery too, it worked.

It was not just the connections on the breadboard that I had in mind. I think it is significant that it worked after you changed the connections to the Arduino

That is called a "clean and reseat" which is like making batteries work by rubbing the ends. Sure, you get a little oxidation (galvanic corrosion) off the contacts, and the thing starts working again, but the "fix" is only temporary, until oxidation rebuilds. When using higher powered devices, more permanent connections must be made. Consider these two methods:

  1. Soldering wires to a prototype breadboard.

  1. Terminal blocks. Using screws to make the wire contact.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.