Jumps in data from joystick

Hi there
I have a joystic that gives me X & Y axis data between 0 -1023

here is the code:

int joyPin1 = A0;                 
 int joyPin2 = A1;                 
 int Xaxis = 0;                  
 int Yaxis = 0;                 
 
 void setup() {
         
  Serial.begin(115200);
 }



 void loop() {
  Xaxis = analogRead(joyPin1);              
  Yaxis = analogRead(joyPin2);
  Serial.print("Xaxis "); 
  Serial.print(Xaxis);
  //Serial.print(",");
  Serial.print("\t");
  Serial.print("Yaxis ");
  Serial.println(Yaxis);
  delay(50);
 }

For some reason I don’t know I am getting jumps in data. So I have a stable number for sum time and suddenly a big jump to a different far away number.

Any guess why does it happen? is it a code problem ?

btw - another issue is that the post window make an error that look like that: picture

Thanks

"So I have a stable number for sum time and suddenly a big jump to a different far away number."

It could be a noisy pot that may need to be squirted with some contact cleaner. It could also be just the reading of the analog pin. Try double reading the pin like below to allow the pin to stabilize some before the second read. Also note that you are trying to read an input that may change in the middle of a read when the pot is being moved.

Xaxis = analogRead(joyPin1);
Xaxis = analogRead(joyPin1);
Yaxis = analogRead(joyPin2);
Yaxis = analogRead(joyPin2);

zoomkat:
"So I have a stable number for sum time and suddenly a big jump to a different far away number."

It could be a noisy pot that may need to be squirted with some contact cleaner. It could also be just the reading of the analog pin. Try double reading the pin like below to allow the pin to stabilize some before the second read. Also note that you are trying to read an input that may change in the middle of a read when the pot is being moved.

Xaxis = analogRead(joyPin1);

Xaxis = analogRead(joyPin1);
Yaxis = analogRead(joyPin2);
Yaxis = analogRead(joyPin2);

Seems to help
Thanks