Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <DynamicHID.h>
  2.  
  3.  
  4. #include <Joystick.h>
  5.  
  6.  
  7.  
  8.  
  9.  
  10. const int UpPin = 2;
  11. const int DownPin = 3;
  12. const int RightPin = 4;
  13. const int LeftPin = 5;
  14.  
  15. int buttonPins[] = {6,7,8,9,10,11};
  16. const int buttonsCount = sizeof(buttonPins)/sizeof(int);
  17.  
  18. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  19. buttonsCount, 0, // Button Count, Hat Switch Count
  20. true, true, false, // X and Y, but no Z Axis
  21. false, false, false, // No Rx, Ry, or Rz
  22. false, false, // No rudder or throttle
  23. false, false, false); // No accelerator, brake, or steering
  24.  
  25. // Last state of the buttons
  26. int lastButtonState[buttonsCount];
  27.  
  28. int lastUpState = 0;
  29. int lastDownState = 0;
  30. int lastLeftState = 0;
  31. int lastRightState = 0;
  32.  
  33. //тут может гнать, если что комменть эту строку
  34. //memset(lastButtonState, 0, sizeof(buttonPins));
  35.  
  36. void setup() {
  37.  
  38. // Initialize Button Pins
  39. pinMode(UpPin, INPUT_PULLUP);
  40. pinMode(DownPin, INPUT_PULLUP);
  41. pinMode(RightPin, INPUT_PULLUP);
  42. pinMode(LeftPin, INPUT_PULLUP);
  43.  
  44. for (int i=0; i<buttonsCount; i++) {
  45. pinMode(buttonPins[i], INPUT_PULLUP);
  46. }
  47.  
  48. // Initialize Joystick Library
  49. Joystick.begin();
  50. Joystick.setXAxisRange(-1, 1);
  51. Joystick.setYAxisRange(-1, 1);
  52. }
  53.  
  54. void loop() {
  55.  
  56. // Read Up
  57. int currentUpState = !digitalRead(UpPin);
  58. int currentDownState = !digitalRead(DownPin);
  59. if(currentUpState !=lastUpState or currentDownState != lastDownState){
  60. if (currentUpState == 1) {
  61. Joystick.setYAxis(-1);
  62. }
  63. else
  64. if (currentDownState == 1) {
  65. Joystick.setYAxis(1);
  66. }
  67. else
  68. Joystick.setYAxis(0);
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75. // Read Down
  76. // int currentDownState = !digitalRead(DownPin);
  77. // if (currentDownState != lastDownState){
  78. // if (currentDownState == 1) {
  79. // Joystick.setYAxis(1);
  80. // } else
  81. // if (currentUpState == 1){
  82. // Joystick.setYAxis(-1);
  83. // }else {
  84. // Joystick.setYAxis(0);
  85. // }
  86. // }
  87.  
  88. // }
  89.  
  90. // Read Left
  91. int currentLeftState = !digitalRead(LeftPin);
  92. if (currentLeftState != lastLeftState){
  93. if (currentLeftState == 1) {
  94. Joystick.setXAxis(-1);
  95. } else {
  96. Joystick.setXAxis(0);
  97. }
  98. }
  99.  
  100. // Read Right
  101. int currentRightState = !digitalRead(RightPin);
  102. if (currentRightState != lastRightState){
  103. if (currentRightState == 1) {
  104. Joystick.setXAxis(1);
  105. } else {
  106. Joystick.setXAxis(0);
  107. }
  108. }
  109.  
  110. // Read buttons values
  111. for (int index = 0; index < buttonsCount; index++){
  112. int currentButtonState = !digitalRead(buttonPins[index]);
  113. if (currentButtonState != lastButtonState[index]){
  114. Joystick.setButton(index, currentButtonState);
  115. }
  116. lastButtonState[index] = currentButtonState;
  117. }
  118. delay(10);
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement