Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 8 button gamepad, switches on digital pins 2-10 https://www.thingiverse.com/thing:3832172
- #include <Joystick.h>
- // requires https://github.com/MHeironimus/ArduinoJoystickLibrary
- Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
- 9, 0, // Button Count, Hat Switch Count
- false, false, false, // No X, Y, Z Axis
- false, false, false, // No Rx, Ry, or Rz
- false, false, // No rudder or throttle
- false, false, false); // No accelerator, brake, or steering
- void setup() {
- // Initialize Button Pins
- pinMode(2, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- pinMode(4, INPUT_PULLUP);
- pinMode(5, INPUT_PULLUP);
- pinMode(6, INPUT_PULLUP);
- pinMode(7, INPUT_PULLUP);
- pinMode(8, INPUT_PULLUP);
- pinMode(9, INPUT_PULLUP);
- pinMode(10, INPUT_PULLUP);
- // Initialize Joystick Library
- Joystick.begin();
- }
- // Last state of the buttons
- int lastButtonState[9] = {0,0,0,0,0,0,0,0,0};
- void loop() {
- // Read pin values
- for (int index = 0; index < 9; index++)
- {
- int currentButtonState = !digitalRead(index + 2);
- if (currentButtonState != lastButtonState[index])
- {
- Joystick.setButton(index, currentButtonState);
- lastButtonState[index] = currentButtonState;
- }
- }
- delay(10);
- }
Add Comment
Please, Sign In to add comment