Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. 1x Arduino Pro Micro (atmega32u4) clone
  2. https://www.aliexpress.com/item/32648920631.html
  3. 1x 8-way Suzo Happ competition joystick (color variety available)
  4. https://na.suzohapp.com/products/joysticks/50-6070-120
  5. 10x Suzo Happ pushbuttons with .187 switches (color variety available)
  6. https://na.suzohapp.com/products/pushbuttons/58-9100-L
  7. 1x Suzo Happ 1p start button with .187 switch
  8. https://na.suzohapp.com/products/pushbuttons/58-9111-L1PLY
  9. ~30x .187 female spade connectors
  10. https://www.amazon.com/dp/B01GYFMCE6
  11.  
  12. 2x Custom modded OpenCADE 3d printed box
  13. https://www.thingiverse.com/thing:3445410
  14. 2x Custom modded OpenCADE 3d printed panel
  15. https://www.thingiverse.com/thing:3491239
  16. 1 standard unmodified box and panel can fit 6+2 buttons and a joystick.
  17.  
  18.  
  19. Write some simple code using Arduino joystick library
  20. https://github.com/MHeironimus/ArduinoJoystickLibrary
  21.  
  22.  
  23. Sample work-in-progress (but functional) code...
  24.  
  25.  
  26.  
  27. #include <Joystick.h>
  28.  
  29. #define BUTTON_COUNT 11
  30. #define JOY_BUTTON_COUNT 4
  31. #define HAT_COUNT 0
  32.  
  33. enum button_names {
  34. BUTTON_SELECT,
  35. BUTTON_START,
  36. BUTTON_A,
  37. BUTTON_B,
  38. BUTTON_X,
  39. BUTTON_Y,
  40. BUTTON_L1,
  41. BUTTON_R1,
  42. BUTTON_L2,
  43. BUTTON_R2,
  44. BUTTON_COIN,
  45. };
  46.  
  47. enum joy_button_names {
  48. JOY_UP,
  49. JOY_RIGHT,
  50. JOY_DOWN,
  51. JOY_LEFT,
  52. };
  53.  
  54. int button_pins[BUTTON_COUNT] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  55. int joy_button_pins[JOY_BUTTON_COUNT] = {18, 19, 20, 21};
  56.  
  57. // Last state of the buttons
  58. int last_button_state[BUTTON_COUNT] = { };
  59. // Last state of the joystick
  60. int last_joy_button_state[JOY_BUTTON_COUNT] = { };
  61.  
  62. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
  63. BUTTON_COUNT, HAT_COUNT, // Button Count, Hat Switch Count
  64. true, true, false, // X and Y, but no Z Axis
  65. false, false, false, // No Rx, Ry, or Rz
  66. false, false, // No rudder or throttle
  67. false, false, false); // No accelerator, brake, or steering
  68.  
  69. void setup() {
  70. // Initialize Button Pins
  71. for (int pin_num = 0; pin_num < BUTTON_COUNT; pin_num++) {
  72. pinMode(button_pins[pin_num], INPUT_PULLUP);
  73. }
  74. for (int pin_num = 0; pin_num < JOY_BUTTON_COUNT; pin_num++) {
  75. pinMode(joy_button_pins[pin_num], INPUT_PULLUP);
  76. }
  77.  
  78. // Initialize Joystick Library
  79. Joystick.begin();
  80. Joystick.setXAxisRange(-1, 1);
  81. Joystick.setYAxisRange(-1, 1);
  82. }
  83.  
  84. void loop() {
  85. // Read pin values
  86. for (int index = 0; index < BUTTON_COUNT; index++)
  87. {
  88. int current_state = !digitalRead(button_pins[index]);
  89. if (current_state != last_button_state[index])
  90. {
  91. switch (index) {
  92. case BUTTON_SELECT:
  93. case BUTTON_START:
  94. case BUTTON_A:
  95. case BUTTON_B:
  96. case BUTTON_X:
  97. case BUTTON_Y:
  98. case BUTTON_L1:
  99. case BUTTON_R1:
  100. case BUTTON_L2:
  101. case BUTTON_R2:
  102. case BUTTON_COIN:
  103. Joystick.setButton(index, current_state);
  104. break;
  105. default:
  106. // How?
  107. break;
  108. }
  109. last_button_state[index] = current_state;
  110. }
  111. }
  112.  
  113. for (int index = 0; index < JOY_BUTTON_COUNT; index++)
  114. {
  115. int current_state = !digitalRead(joy_button_pins[index]);
  116. if (current_state != last_joy_button_state[index])
  117. {
  118. switch (index) {
  119. case JOY_UP:
  120. if (current_state == 1) {
  121. Joystick.setYAxis(-1);
  122. } else {
  123. Joystick.setYAxis(0);
  124. }
  125. break;
  126. case JOY_RIGHT:
  127. if (current_state == 1) {
  128. Joystick.setXAxis(1);
  129. } else {
  130. Joystick.setXAxis(0);
  131. }
  132. break;
  133. case JOY_DOWN:
  134. if (current_state == 1) {
  135. Joystick.setYAxis(1);
  136. } else {
  137. Joystick.setYAxis(0);
  138. }
  139. break;
  140. case JOY_LEFT:
  141. if (current_state == 1) {
  142. Joystick.setXAxis(-1);
  143. } else {
  144. Joystick.setXAxis(0);
  145. }
  146. break;
  147. }
  148. last_joy_button_state[index] = current_state;
  149. }
  150. }
  151.  
  152. delay(10);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement