this code does not hold the position of the servo with arduino
#include <Servo.h>
#define VRX_PIN1 A0 // Arduino pin connected to VRX pin for the first joystick
#define VRY_PIN1 A1 // Arduino pin connected to VRY pin for the first joystick
#define SERVO_X1_PIN 2 // Arduino pin connected to Servo motor 1 for the first pair
#define SERVO_Y1_PIN 3 // Arduino pin connected to Servo motor 2 for the first pair
#define VRX_PIN2 A2 // Arduino pin connected to VRX pin for the second joystick
#define VRY_PIN2 A3 // Arduino pin connected to VRY pin for the second joystick
#define SERVO_X2_PIN 4 // Arduino pin connected to Servo motor 1 for the second pair
#define SERVO_Y2_PIN 5 // Arduino pin connected to Servo motor 2 for the second pair
Servo xServo1;
Servo yServo1;
Servo xServo2;
Servo yServo2;
void setup() {
Serial.begin(9600);
xServo1.attach(SERVO_X1_PIN);
yServo1.attach(SERVO_Y1_PIN);
xServo2.attach(SERVO_X2_PIN);
yServo2.attach(SERVO_Y2_PIN);
}
void loop() {
int xValue1 = analogRead(VRX_PIN1);
int yValue1 = analogRead(VRY_PIN1);
int xValue2 = analogRead(VRX_PIN2);
int yValue2 = analogRead(VRY_PIN2);
int xAngle1 = map(xValue1, 0, 1023, 0, 180);
int yAngle1 = map(yValue1, 0, 1023, 0, 180);
int xAngle2 = map(xValue2, 0, 1023, 0, 180);
int yAngle2 = map(yValue2, 0, 1023, 0, 180);
xServo1.write(xAngle1);
yServo1.write(yAngle1);
xServo2.write(xAngle2);
yServo2.write(yAngle2);
Serial.print("Joystick 1: ");
Serial.print(xValue1);
Serial.print(", ");
Serial.print(yValue1);
Serial.print(" => Servo Motor 1: ");
Serial.print(xAngle1);
Serial.print("°, ");
Serial.print(yAngle1);
Serial.print("° - Joystick 2: ");
Serial.print(xValue2);
Serial.print(", ");
Serial.print(yValue2);
Serial.print(" => Servo Motor 2: ");
Serial.print(xAngle2);
Serial.print("°, ");
Serial.print(yAngle2);
Serial.println("°");
}
What I have tried:
i have tried everything im at the end please help