Created
September 30, 2019 17:05
-
-
Save gcambridge/c41aec46d7f7eab3222393916e5ffdd3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Joystick.h> | |
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, //Set up for joystick | |
1, 0, // button counter values | |
true, true, false, // axes (x,y,z) | |
false, false, false, // no Rx, Ry, or Rz (rotation around respective axis) | |
false, false, // no rudder or throttle | |
false, false, false); // no accelerator, brake, or steering | |
void setup() { | |
// initialize button pins | |
pinMode(2, INPUT_PULLUP); // pin 2 = UP | |
pinMode(3, INPUT_PULLUP); // pin 3 = RIGHT | |
pinMode(4, INPUT_PULLUP); // pin 4 = DOWN | |
pinMode(5, INPUT_PULLUP); // pin 5 = LEFT | |
Joystick.begin(); | |
Joystick.setXAxisRange(-1, 1); | |
Joystick.setYAxisRange(-1, 1); | |
} | |
// button state array | |
int lastButtonState[4] = {0,0,0,0}; | |
void loop() { | |
// read in pin values | |
for (int index = 0; index < 4; index++){ | |
int currentButtonState = !digitalRead(index + 2); | |
if (currentButtonState != lastButtonState[index]){ | |
switch (index) { | |
case 0: // UP | |
if (currentButtonState == 1) { | |
Joystick.setYAxis(-1); | |
} else { | |
Joystick.setYAxis(0); | |
} | |
break; | |
case 1: // RIGHT | |
if (currentButtonState == 1) { | |
Joystick.setXAxis(1); | |
} else { | |
Joystick.setXAxis(0); | |
} | |
break; | |
case 2: // DOWN | |
if (currentButtonState == 1) { | |
Joystick.setYAxis(1); | |
} else { | |
Joystick.setYAxis(0); | |
} | |
break; | |
case 3: // LEFT | |
if (currentButtonState == 1) { | |
Joystick.setXAxis(-1); | |
} else { | |
Joystick.setXAxis(0); | |
} | |
break; | |
} | |
lastButtonState[index] = currentButtonState; | |
} | |
} | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment