So, I was working with a project in which I used a joystick module to control two servos at the same time. The joystick controls the servos in a way that when the joystick is moved along x-axis, both servos move in the same direction but when joystick moves in y-axis, the servos move in different directions. I have this program:
#include <Servo.h>
Servo servo1;
Servo servo2;
int joyx = 0;
int joyy = 5;
int joyval;
int joyvalx;
int joyvaly;
void setup() {
Serial.begin(9600);
servo1.attach(3);
servo2.attach(5);
}
void loop() {
Serial.println(joyvalx + "," + joyvaly);
int x = analogRead(joyx);
int y = analogRead(joyy);
if(x != joyvalx){
joyval = map(x, 0, 1023, 0, 180);
servo1.write(joyval);
servo2.write(joyval);
}
if (y != joyvaly){
joyval = map(y, 0, 1023, 0, 180);
servo1.write(joyval);
servo2.write(1023-joyval);
}
}
However, the servos don't seem to work properly. One servo works fine but the other just gets stuck. Also, if I use the code for only one direction (x-axis or y-axis) by removing the code for the other, then both servos work good. I can't understand the problem. If anyone does, please help me.