Why does Joystick pressbutton not change its state?

Hi!

I want to press the joysticks button and change it from pressed to not pressed and print that accordingly on the monitor.

However the state is always "pressed" even if I dont press the button.
I really don't know why, maybe someone can help me?

const int pressbutton = 2;
int pressbuttonValue;


void setup() {
   pinMode(pressbutton,INPUT_PULLUP); // press button
   Serial.begin(9600); 
  //digitalWrite(pressbutton, LOW);

}


void loop() {
   //pressbuttonValue = digitalRead(pressbutton);

  if(digitalRead(pressbutton) == LOW){
    Serial.println("not pressed");
  }
  else {
    Serial.println("pressed");
  }
  delay(100);
}

How is the button wired ?
A diagram would help

I am using this Joystick: KY-023 Joystick (X-Y-Achsen) - SensorKit

The sw pin (in my code its pressbutton) is connected to A2.

const int pressbutton = 2;

Your code is using pin 2. That is not pin A2

Either move the connection to pin 2 or change the sketch to use pin A2

Where is the other switch contact connected to ?

Oh sorry, yes I meant pin 2.
The others are VRx = A0 and VRy = A1

So the switch is connected to pin 2, not A2 but where is the other switch contact connected to ?

Hello miumiu

Post a schematic simply

Internally to the joystick module it is connect to Ground. The X and Y pots are connected to +5 and Ground.

That would probably mean the button is not connected to Pin 2 or the Ground pin of the joystick is not connected to Arduino Ground.

Note: With 'INPUT_PULLUP' a LOW value means 'pressed' and a HIGH value means 'not pressed', the opposite of what your sketch reports.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.