Click here to Skip to main content
15,850,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this code does not hold the position of the servo with arduino
/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-joystick-servo-motor
 */

#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; // create servo object to control the first servo motor
Servo yServo1; // create servo object to control the second servo motor

Servo xServo2; // create servo object to control the third servo motor
Servo yServo2; // create servo object to control the fourth servo motor

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() {
  // read analog X and Y analog values for the first joystick
  int xValue1 = analogRead(VRX_PIN1);
  int yValue1 = analogRead(VRY_PIN1);

  // read analog X and Y analog values for the second joystick
  int xValue2 = analogRead(VRX_PIN2);
  int yValue2 = analogRead(VRY_PIN2);

  int xAngle1 = map(xValue1, 0, 1023, 0, 180); // scale it to the first servo's angle (0 to 180)
  int yAngle1 = map(yValue1, 0, 1023, 0, 180); // scale it to the second servo's angle (0 to 180)

  int xAngle2 = map(xValue2, 0, 1023, 0, 180); // scale it to the third servo's angle (0 to 180)
  int yAngle2 = map(yValue2, 0, 1023, 0, 180); // scale it to the fourth servo's angle (0 to 180)

  xServo1.write(xAngle1); // rotate first servo motor
  yServo1.write(yAngle1); // rotate second servo motor
  xServo2.write(xAngle2); // rotate third servo motor
  yServo2.write(yAngle2); // rotate fourth servo motor

  // print data to Serial Monitor on Arduino IDE
  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:

C++

i have tried everything im at the end please help
Posted

1 solution

After re-reading your post and giving it some thought.

The analog read is what's probably messing you up, it's not giving a consistent reading, which is normal for ADC.

What you might try is reading, say 10 times and averaging the results to output to your servos.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900