Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2020
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. // 8 speed H shifter using 4 switches, by Akaki Kuumeri
  2. //Pins
  3. // 2: X left
  4. // 3: X right
  5. // 4: X right extreme
  6. // 5: X up
  7. // 6: Y down
  8.  
  9. #define LEFT 0
  10. #define RIGHT 1
  11. #define RIGHTEXTREME 2
  12. #define UP 3
  13. #define DOWN 4
  14.  
  15. #include <Joystick.h>
  16.  
  17. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  18. 8, 0, // Button Count, Hat Switch Count
  19. false, false, false, // No X, Y, Z Axis
  20. false, false, false, // No Rx, Ry, or Rz
  21. false, false, // No rudder or throttle
  22. false, false, false); // No accelerator, brake, or steering
  23.  
  24. void setup() {
  25. // Initialize Button Pins
  26. pinMode(2, INPUT_PULLUP);
  27. pinMode(3, INPUT_PULLUP);
  28. pinMode(4, INPUT_PULLUP);
  29. pinMode(5, INPUT_PULLUP);
  30. pinMode(6, INPUT_PULLUP);
  31. pinMode(7, INPUT_PULLUP);
  32.  
  33. // Initialize Joystick Library
  34. Joystick.begin();
  35.  
  36. }
  37.  
  38. // Last state of the buttons
  39. int lastButtonState[8] = {0,0,0,0,0,0,0,0};
  40. int currentButtonState[8] = {0,0,0,0,0,0,0,0};
  41. int input[5] = {0,0,0,0,0};
  42. int gear = 0; //0 is neutral
  43.  
  44. void loop() {
  45.  
  46. // Read pin values
  47. for (int index = 0; index < 5; index++)
  48. {
  49. input[index] = !digitalRead(index+2);
  50. }
  51. gear = 0;
  52. if (input[UP]) //if stick is up
  53. {
  54. if (input[LEFT]) //left
  55. {
  56. gear = 1;
  57. }
  58. else if (input[RIGHT])
  59. {
  60. if (input[RIGHTEXTREME]) //if also right extreme
  61. {
  62. gear = 7;
  63. }
  64. else
  65. {
  66. gear = 5;
  67. }
  68. }
  69. else
  70. {
  71. gear = 3; //stick is neither left not right, but points up
  72. }
  73. }
  74. else if (input[DOWN])
  75. {
  76. if (input[LEFT])
  77. {
  78. gear = 2;
  79. }
  80. else if (input[RIGHT])
  81. {
  82. if (input[RIGHTEXTREME]) //if also right extreme
  83. {
  84. gear = 8;
  85. }
  86. else
  87. {
  88. gear = 6;
  89. }
  90. }
  91. else
  92. {
  93. gear = 4; //stick is neither left not right, but points down
  94. }
  95. } //otherwise 0, neutral
  96.  
  97. //make all buttons 0
  98. currentButtonState[0] = 0;
  99. currentButtonState[1] = 0;
  100. currentButtonState[2] = 0;
  101. currentButtonState[3] = 0;
  102. currentButtonState[4] = 0;
  103. currentButtonState[5] = 0;
  104. currentButtonState[6] = 0;
  105. currentButtonState[7] = 0;
  106. if (gear != 0) {currentButtonState[gear-1] = 1;} //if in gear, make button number gear-1 active. First gear is 0, second is 1 and so on
  107.  
  108. for (int index = 0; index < 8; index++) //send out button comands
  109. {
  110. if (currentButtonState[index] != lastButtonState[index])
  111. {
  112. Joystick.setButton(index, currentButtonState[index]);
  113. lastButtonState[index] = currentButtonState[index];
  114. }
  115. }
  116.  
  117. delay(10);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement