Joystick SW is flipped

Hey all,

Recently, I picked up some Joysticks to use with my Arduino but I've run into an issue after running the code for a second time: the switch state on whether or not the joystick has been pressed down has been flipped. Here's my code:

int VRx = A0; // X axis pin established
int VRy = A1; // Y axis pin established
int SW = 0; // Switch pin established

int xPosition = 0; // Establishing relevant variables
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;

void setup() {
  Serial.begin(9600); // Serial output for test established
  
  pinMode(VRx, INPUT);
  pinMode(VRy, INPUT);
  pinMode(SW, INPUT_PULLUP); 
  
}

void loop() {
  xPosition = analogRead(VRx);
  yPosition = analogRead(VRy);
  SW_state = digitalRead(SW);
  mapX = map(xPosition, 0, 1023, -512, 512);
  mapY = map(yPosition, 0, 1023, -512, 512);
  
  Serial.print("X: ");
  Serial.print(mapX);
  Serial.print(" | Y: ");
  Serial.print(mapY);
  Serial.print(" | Button: ");
  Serial.println(SW_state);

  delay(100);
  
}

What I mean is that the state of whether or not the button has been pressed down now outputs "1" when in a neutral position but "0" when it's been pressed down. This only occurred after the second run of the code; the first time around the neutral position outputted "0" and "1" when pressed down. Is there any fault in my code, or could it be the joystick?

Thanks in advance.

We can't see how you have the switch wired.

I changed your code like this

int VRx = A0; // X axis pin established
int VRy = A1; // Y axis pin established
int SW = 0; // Switch pin established

int xPosition = 0; // Establishing relevant variables
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;

int Oldx = 1;
int Oldy = 1;
int Oldsw = 1;

void setup() {
  Serial.begin(9600); // Serial output for test established
  
  pinMode(VRx, INPUT);
  pinMode(VRy, INPUT);
  pinMode(SW, INPUT_PULLUP); 
  
}

void loop() {
  xPosition = analogRead(VRx);
  yPosition = analogRead(VRy);
  SW_state = digitalRead(SW);
  if (xPosition != Oldx){
    Oldx = xPosition;
    mapX = map(xPosition, 0, 1023, -512, 512);
    Serial.print("X: ");
    Serial.println(mapX);
  }
  if (yPosition != Oldy){
    Oldy = yPosition; 
    mapY = map(yPosition, 0, 1023, -512, 512);
    Serial.print("Y: ");
    Serial.println(mapY);
  }
  if (SW_state != Oldsw){
    Oldsw = SW_state;
    Serial.print("Button: ");
    Serial.println(SW_state);
  }
  delay(100);
  
}

This way it should be easier to follow the changes.

If you like you can compare the outcome in your installation with a simulation at Wokwi:

https://wokwi.com/projects/325410268671640148

This is a "typical" output after start of the simulation:

X: 0
Y: 0
Button: 0
Button: 1
Button: 0
Button: 1
Button: 0
Button: 1
Y: 512
Y: 0
Button: 0
Button: 1
Button: 0
Button: 1

What you can see is that the button usually starts with "1" (which means HIGH level when not pressed) and "0" = LOW when it is pressed.

The reason why this sketch does not display a " Button: 1" after start is that I initialized "Oldsw = 1;". For joystick x and y I used also a "1" as initial value which leads to the output of X and Y (which both is zero due to the "perfectly calibrated" joystick emulation :wink:

If you change e.g. to "Oldsw = 0;" the sketch of course starts with

X: 0
Y: 0
Button: 1

without any intervention. Feel free to check this sketch with your installation! Good luck.

P.S.: The line pinMode(SW, INPUT_PULLUP); intializes the pin SW to go HIGH with an internal pullup resistor. If the circuit is closed the pin goes to GND (if you have done the correct wiring!):

1 Like

Ah, thank you so much for this! It was very insightful and exactly what I needed!

Joystick wiring to switch is switch connects to ground "0" when pressed. Pullup makes it a "1" when open (unpressed)

1 Like

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