Advertisement
Guest User

joystick_rev0

a guest
Jan 12th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. /* Arduino USB Keyboard HID demo
  2. * Volume+/Volume-/Mute keys
  3. */
  4.  
  5. uint8_t buf[8] = {
  6. 0 }; /* Keyboard report buffer */
  7.  
  8. #define PIN_B 0
  9. #define PIN_A 1
  10. #define PIN_RB 2
  11. #define PIN_LB 4
  12. #define PIN_Y 5
  13. #define PIN_X 6
  14. #define PIN_LEFT 8
  15. #define PIN_DOWN 9
  16. #define PIN_RIGHT 11
  17. #define PIN_UP 12
  18.  
  19. int state = 1;
  20.  
  21. void setup()
  22. {
  23. Serial.begin(9600);
  24. pinMode(PIN_B, INPUT);
  25. pinMode(PIN_A, INPUT);
  26. pinMode(PIN_RB, INPUT);
  27. pinMode(PIN_LB, INPUT);
  28. pinMode(PIN_Y, INPUT);
  29. pinMode(PIN_X, INPUT);
  30. pinMode(PIN_LEFT, INPUT);
  31. pinMode(PIN_DOWN, INPUT);
  32. pinMode(PIN_RIGHT, INPUT);
  33. pinMode(PIN_UP, INPUT);
  34. // enable internal pull-ups
  35. digitalWrite(PIN_B, 1);
  36. digitalWrite(PIN_A, 1);
  37. digitalWrite(PIN_RB, 1);
  38. digitalWrite(PIN_LB, 1);
  39. digitalWrite(PIN_Y, 1);
  40. digitalWrite(PIN_X, 1);
  41. digitalWrite(PIN_LEFT, 1);
  42. digitalWrite(PIN_DOWN, 1);
  43. digitalWrite(PIN_RIGHT, 1);
  44. digitalWrite(PIN_UP, 1);
  45.  
  46. delay(200);
  47. }
  48.  
  49. void loop()
  50. {
  51. state = digitalRead(PIN_B);
  52. if (state != 1) {
  53. buf[2] = 13; // Volume up key
  54. Serial.write(buf, 8); // Send keypress
  55. releaseKey();
  56. }
  57.  
  58. state = digitalRead(PIN_A);
  59. if (state != 1) {
  60. buf[2] = 14; // Volume down key
  61. Serial.write(buf, 8); // Send keypress
  62. releaseKey();
  63. }
  64.  
  65. state = digitalRead(PIN_RB);
  66. if (state != 1) {
  67. buf[2] = 15; // Mute key
  68. Serial.write(buf, 8); // Send keypress
  69. releaseKey();
  70. }
  71. state = digitalRead(PIN_LB);
  72. if (state != 1) {
  73. buf[2] = 18; // Volume up key
  74. Serial.write(buf, 8); // Send keypress
  75. releaseKey();
  76. }
  77.  
  78. state = digitalRead(PIN_Y);
  79. if (state != 1) {
  80. buf[2] = 128; // Volume down key
  81. Serial.write(buf, 8); // Send keypress
  82. releaseKey();
  83. }
  84.  
  85. state = digitalRead(PIN_X);
  86. if (state != 1) {
  87. buf[2] = 12; // Mute key
  88. Serial.write(buf, 8); // Send keypress
  89. releaseKey();
  90. }
  91. state = digitalRead(PIN_LEFT);
  92. if (state != 1) {
  93. buf[2] = 4; // Volume up key
  94. Serial.write(buf, 8); // Send keypress
  95. releaseKey();
  96. }
  97.  
  98. state = digitalRead(PIN_DOWN);
  99. if (state != 1) {
  100. buf[2] = 22; // Volume down key
  101. Serial.write(buf, 8); // Send keypress
  102. releaseKey();
  103. }
  104.  
  105. state = digitalRead(PIN_RIGHT);
  106. if (state != 1) {
  107. buf[2] = 7; // Mute key
  108. Serial.write(buf, 8); // Send keypress
  109. releaseKey();
  110. }
  111. state = digitalRead(PIN_UP);
  112. if (state != 1) {
  113. buf[2] = 26; // Volume up key
  114. Serial.write(buf, 8); // Send keypress
  115. releaseKey();
  116. }
  117.  
  118. }
  119.  
  120. void releaseKey()
  121. {
  122. buf[0] = 0;
  123. buf[2] = 0;
  124. Serial.write(buf, 8); // Release key
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement