Skip to content

Commit 5825fcd

Browse files
committed
Invert joystick Y axes in example
These are provided as options in-case the user is using different joysticks that don't invert the Y axis, and these variables are inverted themselves so that the user doesn't think the example is set up wrong. Little white lie...
1 parent 7ae6a11 commit 5825fcd

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

examples/GamepadPins/GamepadPins.ino

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@
4242
#include <XInput.h>
4343

4444
// Setup
45+
const boolean UseLeftJoystick = true; // set to false to disable left joystick
46+
const boolean InvertLeftYAxis = false; // set to true to use inverted left joy Y
47+
48+
const boolean UseRightJoystick = true; // set to false to disable right joystick
49+
const boolean InvertRightYAxis = false; // set to true to use inverted right joy Y
50+
51+
const boolean UseTriggerButtons = true; // set to false if using analog triggers
52+
4553
const int ADC_Max = 1023; // 10 bit
46-
const boolean UseLeftJoystick = true; // set to false to disable left joystick
47-
const boolean UseRightJoystick = true; // set to false to disable right joystick
48-
const boolean UseTriggerButtons = true; // set to false if using analog triggers
4954

5055
// Joystick Pins
5156
const int Pin_LeftJoyX = A0;
@@ -178,6 +183,13 @@ void loop() {
178183
int leftJoyX = analogRead(Pin_LeftJoyX);
179184
int leftJoyY = analogRead(Pin_LeftJoyY);
180185

186+
// White lie here... most generic joysticks are typically
187+
// inverted by default. If the "Invert" variable is false
188+
// then we need to do this transformation.
189+
if (InvertLeftYAxis == false) {
190+
leftJoyY = ADC_Max - leftJoyY;
191+
}
192+
181193
XInput.setJoystick(JOY_LEFT, leftJoyX, leftJoyY);
182194
}
183195

@@ -186,6 +198,10 @@ void loop() {
186198
int rightJoyX = analogRead(Pin_RightJoyX);
187199
int rightJoyY = analogRead(Pin_RightJoyY);
188200

201+
if (InvertRightYAxis == false) {
202+
rightJoyY = ADC_Max - rightJoyY;
203+
}
204+
189205
XInput.setJoystick(JOY_RIGHT, rightJoyX, rightJoyY);
190206
}
191207

0 commit comments

Comments
 (0)