Problem in the input of a HW-504 joystick

Im trying to control 2 servos with a HW-504 joystick (its my first project), but the interaction between them is kinda random (sometimes works, sometimes noope:() in the serial port de data from the joystick is showing all the possible data (0-1023), so I connected the joystick directly on the arduino board but serial port keeps de same. :frowning:

Hi! Its my first interaction here, I read the guidelines but sorry if Im doing something wrong, also Im not a english native too.

#include <Servo.h>

#define pinServo1 8  
#define pinServo2 12

#define pinPotenciometroX A1
#define pinPotenciometroY A4
#define botao 2

Servo servo1;

Servo servo2;

void setup() 
{
  pinMode(botao, INPUT_PULLUP); 
  
  servo1.attach(pinServo1);
  servo2.attach(pinServo2);
  Serial.begin(9600);
}

void loop() 
{
  int lidox    = analogRead(pinPotenciometroX);
  int lidoy    = analogRead(pinPotenciometroY);
  
  byte angulox = map (lidox, 0, 1023, 0, 179);
  byte anguloy = map (lidoy, 0, 1023, 0, 179);
  bool status_switch = digitalRead(botao); 
  
  servo1.write(angulox);  
  servo2.write(anguloy);   

  delay(15);

  Serial.print( "A0: ");
  Serial.print(lidox );
  Serial.print(" A1: ");
  Serial.print(lidoy );
  Serial.print(" Angulo x: ");
  Serial.print(angulox );
  Serial.print(" Angulo y: ");
  Serial.print(anguloy ) ;

if(status_switch)
{
  Serial.println("solto");
}
  
  else {
      Serial.println ("pressionado");
  }
}

Wouldn't it be sensible to call them "servoX" and "servoY"?

This is no good. To read 1023, 10 bits an integer is needed.

  byte angulox = map (lidox, 0, 1023, 0, 179);
  byte anguloy = map (lidoy, 0, 1023, 0, 179);

Change to int anuglox, int anguloy.

Railroader:
This is no good. To read 1023, 10 bits an integer is needed.

  byte angulox = map (lidox, 0, 1023, 0, 179);

byte anguloy = map (lidoy, 0, 1023, 0, 179);




Change to int anuglox, int anguloy.

0 to 179 fits quite well in a byte. (Which is an integer)