This has been quite annoying for me and I cannot find any issues with the circuit or the code, and I even tried to literally copy the code and the circuit for joystick from online multiple times in order to get it working, yet the result is always the same.
I am constantly getting 0 for X.
Other stuff (Y axis and the button) is working as intended. I seriously don't understand why this is happening. This was the code I got from here:
int xPin = A1;
int yPin = A0;
int buttonPin = 2;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
//activate pull-up resistor on the push-button pin
pinMode(buttonPin, INPUT_PULLUP);
// For versions prior to Arduino 1.0.1
// pinMode(buttonPin, INPUT);
// digitalWrite(buttonPin, HIGH);
}
void loop() {
xPosition = analogRead(xPin);
yPosition = analogRead(yPin);
buttonState = digitalRead(buttonPin);
Serial.print("X: ");
Serial.print(xPosition);
Serial.print(" | Y: ");
Serial.print(yPosition);
Serial.print(" | Button: ");
Serial.println(buttonState);
delay(100); // add some delay between reads
}
And this was the output:
X: 0 | Y: 510 | Button: 1
X: 0 | Y: 510 | Button: 1
X: 0 | Y: 510 | Button: 1
X: 0 | Y: 510 | Button: 1
X: 0 | Y: 510 | Button: 1
...
Does anyone know about this issue, or even better, a solution?