How to Keep Servo in One place using a Joystick?

Hello everyone, so I have a project that involves an arduino nano, some servos and a joystick. Basically what I want to do is be able to move the joystick up and have the servo stay that way until I move the joystick down and bring it back to the original position. I have no idea how to code this so I need some help lol. If you're wondering what it looks like, the youtube video below is what I followed.

After reading the "How to get the best out of the forum" post (at the head of every forum category), post the code you have, using code tags. Explain what it should do, and what it does instead.

Yes, that's how it works.
If you are having a problem with the servo returning when you let the joystick go, take off the return spring.
Difficult to say really wht problem you may be encounting.
See servo example in the Arduino IDE or in perhaps a million places online.

How do I take off the return spring? Heres the code for you

#include <Servo.h>
#define joyX A0

Servo servo1;

int xValue, yValue;
int pos = 180;

void setup() {
Serial.begin(9600);
servo1.attach(9);
servo1.write(90);

}

void loop() {
xValue = analogRead(joyX);

xValue= map (xValue, 0, 180, 0, 180);

servo1. write(xValue);

Serial.print("X Axis: ");
Serial.print(xValue);
Serial.print("\t");
Serial.print("Y Axis :");
Serial.print(yValue);
Serial.println(yValue);
delay(100);
}

It looks like you tried to follow the code in the video but missed something.

The code in the video works, so what doesn’t yours do ?

It’s not complicated.

Its working fine but I want it so that it stays in place when you move it

I don't know how to code I just need to know what to change with this one to make it stay in place

Take your joystick apart, remove the return spring, and put it back together.

…or just read the min /max value of the joystick.

Use that to move the motor.
How you return it is up to you.

How would I do that?

What is that position? What is the maximum position that you want to hold? What is the ADC reading when the joystick is at "neutral"? When at full up position?

I want it to be it to go from 0 to 180 degrees and the neutral position for the x axis is at 500 when the joystick is left alone. I want it to stay at 180 until I bring the joystick back down.

What is the ADC reading when the joystick is at the full up position and the servo should be at 180 degrees?

I don't know how I would find that out

Connect the joystick to pin A7 and run this:

void setup() {
  Serial.begin(9600);

}
void loop() {
  Serial.println(analogRead(A7));
  delay(500);
}

What does it read at neutral and full up?

This does nothing.

To stop the servo when the joystick goes back to center, use the joystick for relative motion, not absolute.

void loop() {
  static int servoPosition = 90; 

  int xOffset = map (analogRead(joyX), 0, 1023, -5, +5);
  servoPosition += xOffset;
  servoPosition = constrain(servoPosition, 0, 180);
  servo1. write(servoPosition);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.