How to keep servo in place using joystick?

Hello everyone, I have a project I'm working on involving an arduino nano, a joystick and a couple servos. What I want to do is have the joystick move the servo up 180 degrees and have it stay there until I move the joystick down and set it back to it's original position. I have no idea how to code this so I could use some help lol.

Welcome to the forum

Do you know how to read a potentiometer using an analogue pin on the Arduino ?

Dow you know how to control a servo and make it move to an angle ?

No to either lol

I copied someones code on youtube pretty much

What did that code do and what didn't it do? Post it! A wiring diagram will also be useful.

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

What about the question in reply #5? Read the topic at the top of the section, "How to get the best from this forum".

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

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