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.