Skip to content

Commit 35c1739

Browse files
committed
Update GamepadPins example with joystick invert
In place of calculating this "manually" with the ADC value
1 parent 98c8cd8 commit 35c1739

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

examples/GamepadPins/GamepadPins.ino

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
* and all of the main buttons.
2929
*
3030
* * Joysticks should be your typical 10k dual potentiometers.
31+
* To prevent random values caused by floating inputs,
32+
joysticks are disabled by default.
3133
* * Triggers can be either analog (pots) or digital (buttons).
3234
* Set the 'TriggerButtons' variable to change between the two.
3335
* * Buttons use the internal pull-ups and should be connected
@@ -187,24 +189,22 @@ void loop() {
187189

188190
// White lie here... most generic joysticks are typically
189191
// inverted by default. If the "Invert" variable is false
190-
// then we need to do this transformation.
191-
if (InvertLeftYAxis == false) {
192-
leftJoyY = ADC_Max - leftJoyY;
193-
}
192+
// then we'll take the opposite value with 'not' (!).
193+
boolean invert = !InvertLeftYAxis;
194194

195-
XInput.setJoystick(JOY_LEFT, leftJoyX, leftJoyY);
195+
XInput.setJoystickX(JOY_LEFT, leftJoyX);
196+
XInput.setJoystickY(JOY_LEFT, leftJoyY, invert);
196197
}
197198

198199
// Set right joystick
199200
if (UseRightJoystick == true) {
200201
int rightJoyX = analogRead(Pin_RightJoyX);
201202
int rightJoyY = analogRead(Pin_RightJoyY);
202203

203-
if (InvertRightYAxis == false) {
204-
rightJoyY = ADC_Max - rightJoyY;
205-
}
204+
boolean invert = !InvertRightYAxis;
206205

207-
XInput.setJoystick(JOY_RIGHT, rightJoyX, rightJoyY);
206+
XInput.setJoystickX(JOY_RIGHT, rightJoyX);
207+
XInput.setJoystickY(JOY_RIGHT, rightJoyY, invert);
208208
}
209209

210210
// Send control data to the computer

0 commit comments

Comments
 (0)