Skip to content

Commit 7ae6a11

Browse files
committed
Disable joystick option in gamepad example
If there is no joystick connected these pins float and the joysticks seem to be behaving erratically. This adds a pair of booleans to optionally disable them.
1 parent 128e0f7 commit 7ae6a11

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

examples/GamepadPins/GamepadPins.ino

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141

4242
#include <XInput.h>
4343

44-
// Range Setup
44+
// Setup
4545
const int ADC_Max = 1023; // 10 bit
46-
const boolean UsingTriggerButtons = true; // set to false if using analog triggers
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
4749

4850
// Joystick Pins
4951
const int Pin_LeftJoyX = A0;
@@ -78,7 +80,7 @@ const int Pin_DpadRight = 13;
7880

7981
void setup() {
8082
// If using buttons for the triggers, use internal pull-up resistors
81-
if (UsingTriggerButtons == true) {
83+
if (UseTriggerButtons == true) {
8284
pinMode(Pin_TriggerL, INPUT_PULLUP);
8385
pinMode(Pin_TriggerR, INPUT_PULLUP);
8486
}
@@ -152,7 +154,7 @@ void loop() {
152154
XInput.setDpad(dpadUp, dpadDown, dpadLeft, dpadRight);
153155

154156
// Set XInput trigger values
155-
if (UsingTriggerButtons == true) {
157+
if (UseTriggerButtons == true) {
156158
// Read trigger buttons
157159
boolean triggerLeft = !digitalRead(Pin_TriggerL);
158160
boolean triggerRight = !digitalRead(Pin_TriggerR);
@@ -171,16 +173,21 @@ void loop() {
171173
XInput.setTrigger(TRIGGER_RIGHT, triggerRight);
172174
}
173175

174-
// Read analog joystick values
175-
int leftJoyX = analogRead(Pin_LeftJoyX);
176-
int leftJoyY = analogRead(Pin_LeftJoyY);
176+
// Set left joystick
177+
if (UseLeftJoystick == true) {
178+
int leftJoyX = analogRead(Pin_LeftJoyX);
179+
int leftJoyY = analogRead(Pin_LeftJoyY);
177180

178-
int rightJoyX = analogRead(Pin_RightJoyX);
179-
int rightJoyY = analogRead(Pin_RightJoyY);
181+
XInput.setJoystick(JOY_LEFT, leftJoyX, leftJoyY);
182+
}
183+
184+
// Set right joystick
185+
if (UseRightJoystick == true) {
186+
int rightJoyX = analogRead(Pin_RightJoyX);
187+
int rightJoyY = analogRead(Pin_RightJoyY);
180188

181-
// Set XInput joystick values
182-
XInput.setJoystick(JOY_LEFT, leftJoyX, leftJoyY);
183-
XInput.setJoystick(JOY_RIGHT, rightJoyX, rightJoyY);
189+
XInput.setJoystick(JOY_RIGHT, rightJoyX, rightJoyY);
190+
}
184191

185192
// Send control data to the computer
186193
XInput.send();

0 commit comments

Comments
 (0)