servo motor arduino

Thread Starter

nefoundlanderthefirst

Joined Oct 13, 2023
1
i want this code to hold the servos position please help this is the code

C-like:
#include <Servo.h>




#define VRX_PIN1 A0


#define VRY_PIN1 A1


#define SERVO_X1_PIN 2


#define SERVO_Y1_PIN 3




#define VRX_PIN2 A2


#define VRY_PIN2 A3


#define SERVO_X2_PIN 4


#define SERVO_Y2_PIN 5




Servo xServo1;


Servo yServo1;


Servo xServo2;


Servo yServo2;




int xAngle1 = 90;


int yAngle1 = 90;


int xAngle2 = 90;


int yAngle2 = 90;




int deadZone = 10; // Dode zone waar kleine bewegingen genegeerd worden




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




// Implementeer de dode zone


if (abs(xValue1 - 512) > deadZone) {


xAngle1 = map(xValue1, 0, 1023, 0, 180);


}


if (abs(yValue1 - 512) > deadZone) {


yAngle1 = map(yValue1, 0, 1023, 0, 180);


}


if (abs(xValue2 - 512) > deadZone) {


xAngle2 = map(xValue2, 0, 1023, 0, 180);


}


if (abs(yValue2 - 512) > deadZone) {


yAngle2 = map(yValue2, 0, 1023, 0, 180);


}




xServo1.write(xAngle1);


yServo1.write(yAngle1);


xServo2.write(xAngle2);


yServo2.write(yAngle2);




// Print de gegevens naar de Serial Monitor


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("°");


}
 
Last edited by a moderator:
Top