Weird joystick action

Hi,
I connected a joystick to a Leonardo, and it works fine but sometimes i find that it starts sending input as if i was tilting the joystick to the right bottom corner even if i’m not touching anything. Like this:

Here is the code i’m using. It contains also a little macro that is activate by a toggle switch.

#include <Mouse.h>
#include <Keyboard.h>
#include <Joystick.h>

Joystick_ Joystick;

int8_t joystickType = 0x02;
float Multiplier = 1.2;

bool includeXAxis = true;

bool includeYAxis = true;

const int Analog1 = 0;

const int Analog2 = 1;

int XValue = 0;

int YValue = 0;

int inPin = 2;         // the number of the input pin

int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = HIGH;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  Joystick.begin();

  delay(1000);

  pinMode(inPin, INPUT_PULLUP);
}

void loop() {
  XValue = analogRead(Analog1);

  YValue = analogRead(Analog2);

  XValue = XValue * Multiplier;
  YValue = YValue * Multiplier;


  Joystick.setXAxis(XValue); //YAW

  Joystick.setYAxis(YValue); // Throttle

    reading = digitalRead(inPin);

    // if the input just went from HIGH and LOW and we've waited long enough
    // to ignore any noise on the circuit, toggle the output pin and remember
    // the time
    if (reading == LOW && previous == HIGH && millis() - time > debounce) {
      if (state == LOW)
        state = HIGH;
      else
        state = LOW;

      time = millis();
    }
    previous = reading;
    if (state == HIGH) {
      uint16_t randomDelay = random(350, 565);
      Keyboard.press('y');
      delay (randomDelay);
      Keyboard.releaseAll();
      int32_t randomDelay2 = random(1845684, 2365644);
      delay (randomDelay2) ;
    }
  }

Anyone please?
thanks

XValue = (analogRead(Analog1));

YValue = (analogRead(Analog2));

XValue = (XValue * Multiplier);
YValue = (YValue * Multiplier);

(You) (do) (like) (unnecessary) (parentheses) (,) (don't) (you) (?)

{
  reading = digitalRead(inPin);

{and} {unnecessary} {curly} {braces}.

You don't really need three variables, state, reading, and previous, to determine whether a switch has changed state.

Could it be because i did not connect the button at pin 2?

The type, INPUT_PULLUP, ensures that even though there is nothing connected to the pin it will always be in a known state (i.e. not floating).

But, since nothing is supposed to happen unless the switch connected to the pin changes state, it doesn't make any sense to read the pin when nothing is connected to it.

Your piss-poor indenting and extra curly braces makes your code very hard to follow. Using Tools + Auto Format before posting code would at least make it look like you knew what you were doing. So, I might have missed something.

Ok thanks for reply, now i removed the unneeded brackets.
Any one please? I had to solder the wires of the joystick, and it was hard because the connections were too close, maybe they are making contact some times? Or the board is damaged somehow?
edit:
I tried a different joystick that’s easy to connect to arduino and the issue still happens. Sometimes, after turning the arduino on, it ramains frozen like this: https://ibb.co/nwVRZT and after resetting the board it starts working properly. This happens even with just the basic joystick code:

#include <Joystick.h>

Joystick_ Joystick;

float Multiplier = 1.2;





bool includeXAxis = true;

bool includeYAxis = true;




const int Analog1 = 0;

const int Analog2 = 1;




int XValue = 0;

int YValue = 0;



void setup() {

Joystick.begin();

delay(1000);

}

void loop() {



XValue = (analogRead(Analog1));

YValue = (analogRead(Analog2));

XValue = (XValue * Multiplier);
YValue = (YValue * Multiplier);


Joystick.setXAxis(XValue); //YAW

Joystick.setYAxis(YValue); // Throttle

}

At this point i think the board is somehow damaged?

Anyone please?