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
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)
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)
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);
}
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: