2 axis Joystick with Arduino Leonardo

Hello, My X axis work fine but the Y axis is dead. on window game controller the little cross moves left to right fine, but wont move up and down. Just jumps a little bit and freezes. Both my encoders test fine. I paste my code here. My apologies, please tell me if there is another way to post code.
#include <Bounce2.h>
#include <Joystick.h>
#include <Encoder.h>
//#include <PluggableUSB.h>
// or
//#include <USB-MIDI.h>

// Define button pins
const int buttonAPin = 8;
const int buttonBPin = 9;
const int joystickXPinA = 2;
const int joystickXPinB = 3;
const int joystickYPinA = 4;
const int joystickYPinB = 5;

// Define the joystick object
Joystick_ Joystick;

// Define bounce objects to handle button debouncing
Bounce buttonABounce = Bounce(buttonAPin, 10);
Bounce buttonBBounce = Bounce(buttonBPin, 10);
//--------------------------------------------------
// Define the two encoder pins
const int encoder1PinA = 2;
const int encoder1PinB = 3;
const int encoder2PinA = 4;
const int encoder2PinB = 5;

// Create instances of the Encoder class
Encoder encoder1(encoder1PinA, encoder1PinB);
Encoder encoder2(encoder2PinA, encoder2PinB);

//-------------------------------------------------------

void setup() {
// Initialize the joystick library
Joystick.begin();

// Set button pins as inputs with internal pull-up resistors
pinMode(buttonAPin, INPUT_PULLUP);
pinMode(buttonBPin, INPUT_PULLUP);
}

void loop() {
// Update the joystick axes
int joystickX = digitalRead(joystickXPinA);
//int joystickX = digitalRead(joystickXPinB);
int joystickY = digitalRead(joystickYPinA);
//--------------------------------------------------
// Read encoder positions
int encoder1Value = encoder1.read();
int encoder2Value = encoder2.read();
//----------------------------------------------------
// Map joystick values to the joystick range (0 to 1023) to match HID joystick format
// Map encoder values to the joystick range (0 to 1023) to match HID joystick format
joystickX = map(encoder1Value, 0, 1023, 0, 1023);
joystickY = map(encoder2Value, 0, 1023, 0, 1023);

//--------------------------------------------------------------------------------------
// Print the values to the Serial Monitor
Serial.print("Encoder 1: ");
Serial.print(encoder1Value);
Serial.print("\tEncoder 2: ");
Serial.println(encoder2Value);

delay(100); // Adjust delay as needed

//-----------------------------------------------------------------------------------

// Update the joystick position
Joystick.setXAxis(joystickX);
Joystick.setYAxis(joystickY);

//------------------------------------------------------------

// Read button states with debouncing
buttonABounce.update();
buttonBBounce.update();

// Check if buttons are pressed and send joystick button states
if (buttonABounce.fell()) {
Joystick.setButton(1, 1);
} else if (buttonABounce.rose()) {
Joystick.setButton(1, 0);
}

if (buttonBBounce.fell()) {
Joystick.setButton(2, 1);
} else if (buttonBBounce.rose()) {
Joystick.setButton(2, 0);
}

// Add more buttons and code for additional features as needed

// A small delay to avoid sending data too quickly
delay(10);
}

You might want to look at this How to get the best out of this forum before you proceed any further. It tells you how to post code correctly.

You also need to post your schematic for us to make sense of your code.

  int joystickX = digitalRead(joystickXPinA);
 
  int encoder1Value = encoder1.read();
  
  joystickX = map(encoder1Value, 0, 1023, 0, 1023);

what is this? where you catch this garbage?

He forgot to mention "ChatGPT wrote this almost perfectly working code for me"

C'mon guys, please give me a break. I read you post about the code tabs and auto format, so I will post my code again with code tabs. I have also included a circuit diagram. I have not included any error messages, because I got no errors. I studied the code for the joystick (from chatGPT) which has analog input and I studied the code on encoders, and I tried to change the joystick code to read encoder values. I sweated this for 2 days, and got it to compile and upload, but the second axis does not work. So I am requesting your help.`

[code]

#include <Bounce2.h>
#include <Joystick.h>
#include <Encoder.h>
//#include <PluggableUSB.h>
// or
//#include <USB-MIDI.h>


// Define button pins
const int buttonAPin = 8;
const int buttonBPin = 9;
const int joystickXPinA = 2;
const int joystickXPinB = 3;
const int joystickYPinA = 4;
const int joystickYPinB = 5;

// Define the joystick object
Joystick_ Joystick;

// Define bounce objects to handle button debouncing
Bounce buttonABounce = Bounce(buttonAPin, 10);
Bounce buttonBBounce = Bounce(buttonBPin, 10);
//--------------------------------------------------
// Define the two encoder pins
const int encoder1PinA = 2;
const int encoder1PinB = 3;
const int encoder2PinA = 4;
const int encoder2PinB = 5;

// Create instances of the Encoder class
Encoder encoder1(encoder1PinA, encoder1PinB);
Encoder encoder2(encoder2PinA, encoder2PinB);

//-------------------------------------------------------

void setup() {
  // Initialize the joystick library
  Joystick.begin();

  // Set button pins as inputs with internal pull-up resistors
  pinMode(buttonAPin, INPUT_PULLUP);
  pinMode(buttonBPin, INPUT_PULLUP);
}

void loop() {
  // Update the joystick axes
  int joystickX = digitalRead(joystickXPinA);
  //int joystickX = digitalRead(joystickXPinB);
  int joystickY = digitalRead(joystickYPinA);
  //--------------------------------------------------
  // Read encoder positions
  int encoder1Value = encoder1.read();
  int encoder2Value = encoder2.read();
  //----------------------------------------------------
  // Map joystick values to the joystick range (0 to 1023) to match HID joystick format
  // Map encoder values to the joystick range (0 to 1023) to match HID joystick format
  joystickX = map(encoder1Value, 0, 1023, 0, 1023);
  joystickY = map(encoder2Value, 0, 1023, 0, 1023);


  //--------------------------------------------------------------------------------------
  // Print the values to the Serial Monitor
  Serial.print("Encoder 1: ");
  Serial.print(encoder1Value);
  Serial.print("\tEncoder 2: ");
  Serial.println(encoder2Value);

  delay(100);  // Adjust delay as needed

  //-----------------------------------------------------------------------------------

  // Update the joystick position
  Joystick.setXAxis(joystickX);
  Joystick.setYAxis(joystickY);

  //------------------------------------------------------------

  // Read button states with debouncing
  buttonABounce.update();
  buttonBBounce.update();

  // Check if buttons are pressed and send joystick button states
  if (buttonABounce.fell()) {
    Joystick.setButton(1, 1);
  } else if (buttonABounce.rose()) {
    Joystick.setButton(1, 0);
  }

  if (buttonBBounce.fell()) {
    Joystick.setButton(2, 1);
  } else if (buttonBBounce.rose()) {
    Joystick.setButton(2, 0);
  }

  // Add more buttons and code for additional features as needed

  // A small delay to avoid sending data too quickly
  delay(10);
}
`
[code]

Here is my circuit diagram, encoder 1 A &B on digital 2 and 3 and encoder 2 A&B on pins3 &4, common 5V and Gnd
image

My standard advice:

Keep asking the robot until he/she/it gives you the correct result you expect.

One answer could probably be 42.

You are forcing the encoder to use a non-interrupt approach, I think.

If you can't use interrupts, then the polling must be very frequent or

  // With any substantial delay added, Encoder can only track
  // very slow motion.  You may uncomment this line to see
  // how badly a delay affects your encoder.

So you have to check, interrupts only work on certain pins. If you can't use interrupt able pins, you need to eliminate any substantial delay() calls in your code, so that encoder updates come rapidly enough to follow the rotary encoder signals.

HTH

a7