Joystick 2 servos x and y postitions

Hi, I recently started using Arduino since I want to make a submarine. But I come into some issues using the code for a joystick which controls two servos. the Joystick outputs 3 axis to the servo. But I only want two. is there a way to get rid of the corner axis in the code and make it so the servos go left and right separately depending which way you move the joystick? " I only want two axis potentiometer" and the joysticks are very sensitive is there a way to implement some dead zone within the joystick? I am using SG90 servos and the default Arduino joystick module, thanks. here is the code I was using.

#include <Servo.h>

//Servo objects created to control the servos
Servo myServo1;
Servo myServo2;

int servo1 = 3; //Digital PWM pin used by the servo 1
int servo2 = 5; //Digital PWM pin used by the servo 2
int joyX = 0;   //Analog pin to which the joystick (X) is connected
int joyY = 1;   //Analog pin to which the joystick (Y) is connected


const byte JoystickCount = 4;
const byte AxisCount = JoystickCount * 2;



void setup(){
  myServo1.attach(servo1);
  myServo2.attach(servo2);
}

void loop(){

  
  int valX = analogRead(joyX); //Read the joystick X value (value between 0 and 1023)
  int valY = analogRead(joyY); //Read the joystick Y value (value between 0 and 1023)

  valX = map(valX, 5, 1023, 10, 170); //Scale the joystick X value to use it with the servo
  valY = map(valY, 5, 1023, 10, 170); //Scale the joystick X value to use it with the servo

  //Sets the servo position according to the scaled values.
  myServo1.write(valX);   
  myServo2.write(valY);

  delay(5);
}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask like a link to your joystick, description of your circuit etc).

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It’s barely readable as it stands. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)

thanks i fixed it :slight_smile:

Can I have a look at the joystick, please? Specs, if possible, too?

https://www.jaycar.com.au/arduino-compatible-x-and-y-axis-joystick-module/p/XC4422?pos=2&queryId=4f83c6f6ae45e7b611002494f35fa1bf this is were i bought it from and i am also using a Arduino Uno r3.

You mentioned 3 axis. I only see 2 axis on that joystick.

i understand that but when you move the joystick to the corners both of the servos move to certain positions. but i don't want the servos to move when you put the joystick at the corners not for them to move together when you move the knob to the corner of the joystick.

Define it for one axis at a time:

If Y > -m
 {
  Xout = X
 }
else
 {
  Xout = -m
 }

If Y < m
 {
  Xout = X
 }
else
 {
  Xout = m
 }

constant "m" defines the width of the exclusion zone.

Edit - I messed that up... trying to code too fast. Perhaps you get the idea, see how you can use the Y position to limit X to an inclusion zone.

Need the second cup of coffee...

thank you, I will try that.

Normally, the corner behavior is desirable for RC models, but if you really don't want it, it might be easiest to get a second joystick.

if you could can you please explain it in kindergarten terms, i am very new to all this arduino stuff! thanks

Are you new to math? It's just math.

i am only using it for a simple rudder system on a submarine. and my designed controller doesn't have enough room for 2 joysticks.

Desired behaviour as I understand it:

If the Y position is greater or less than zero by an amount "m", then the X position should be set to X, or m, whichever is lesser. Or, -X or -m, whichever is greater, for the negative values of X.

Before going on, is that correct?

that looks goodi think :sweat_smile:

Then code that. Have a try, and post it here. People will help you with any problems.

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