Guest User

Untitled

a guest
May 15th, 2020
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1.        
  2.         // 8 button gamepad, switches on digital pins 2-10 https://www.thingiverse.com/thing:3832172
  3.        
  4.         #include <Joystick.h>
  5.         // requires https://github.com/MHeironimus/ArduinoJoystickLibrary
  6.        
  7.         Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  8.           9, 0,                  // Button Count, Hat Switch Count
  9.           false, false, false,   // No X, Y, Z Axis
  10.           false, false, false,   // No Rx, Ry, or Rz
  11.           false, false,          // No rudder or throttle
  12.           false, false, false);  // No accelerator, brake, or steering
  13.        
  14.         void setup() {
  15.           // Initialize Button Pins
  16.           pinMode(2, INPUT_PULLUP);
  17.           pinMode(3, INPUT_PULLUP);
  18.           pinMode(4, INPUT_PULLUP);
  19.           pinMode(5, INPUT_PULLUP);
  20.           pinMode(6, INPUT_PULLUP);
  21.           pinMode(7, INPUT_PULLUP);
  22.           pinMode(8, INPUT_PULLUP);
  23.           pinMode(9, INPUT_PULLUP);
  24.           pinMode(10, INPUT_PULLUP);
  25.        
  26.           // Initialize Joystick Library
  27.           Joystick.begin();
  28.        
  29.         }
  30.        
  31.         // Last state of the buttons
  32.         int lastButtonState[9] = {0,0,0,0,0,0,0,0,0};
  33.        
  34.         void loop() {
  35.        
  36.           // Read pin values
  37.           for (int index = 0; index < 9; index++)
  38.           {
  39.             int currentButtonState = !digitalRead(index + 2);
  40.             if (currentButtonState != lastButtonState[index])
  41.             {
  42.               Joystick.setButton(index, currentButtonState);
  43.               lastButtonState[index] = currentButtonState;
  44.             }
  45.           }
  46.        
  47.           delay(10);
  48.         }
Add Comment
Please, Sign In to add comment