Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #include <Keyboard.h>
  2. #include <Mouse.h>
  3.  
  4. #define DELAY 5 // Delay per loop in ms
  5. #define HITBTN 700
  6.  
  7. enum PinAssignments
  8. {
  9. encoderPinA = 0,
  10. encoderPinB = 1,
  11. encoderPinC = 3,
  12. encoderPinD = 2,
  13.  
  14. buttonWhiteL = A1,
  15. buttonWhiteR = A0,
  16. buttonYellowL = A3,
  17. buttonYellowR = A2,
  18. buttonGreenL = A5,
  19. buttonGreenR = A4,
  20. buttonBlueL = 8,
  21. buttonBlueR = 9,
  22. buttonRed = 10,
  23. };
  24. //This is up to your pin wiring
  25.  
  26. int encoderPos[] = {0, 0};
  27. static boolean rotating[] = {false, false};
  28.  
  29. boolean A_set = false;
  30. boolean B_set = false;
  31. boolean C_set = false;
  32. boolean D_set = false;
  33.  
  34. void setup()
  35. {
  36. Keyboard.begin();
  37. Mouse.begin();
  38.  
  39. pinMode(buttonWhiteL, INPUT_PULLUP);
  40. pinMode(buttonWhiteR, INPUT_PULLUP);
  41. pinMode(buttonYellowL, INPUT_PULLUP);
  42. pinMode(buttonYellowR, INPUT_PULLUP);
  43. pinMode(buttonGreenL, INPUT_PULLUP);
  44. pinMode(buttonGreenR, INPUT_PULLUP);
  45. pinMode(buttonBlueL, INPUT_PULLUP);
  46. pinMode(buttonBlueR, INPUT_PULLUP);
  47. pinMode(buttonRed, INPUT_PULLUP);
  48.  
  49. delay(1000);
  50.  
  51. pinMode(encoderPinA, INPUT_PULLUP);
  52. pinMode(encoderPinB, INPUT_PULLUP);
  53. pinMode(encoderPinC, INPUT_PULLUP);
  54. pinMode(encoderPinD, INPUT_PULLUP);
  55.  
  56. attachInterrupt(0, doEncoderC, CHANGE);
  57. attachInterrupt(1, doEncoderD, CHANGE);
  58.  
  59. attachInterrupt(2, doEncoderA, CHANGE);
  60. attachInterrupt(3, doEncoderB, CHANGE);
  61.  
  62. delay(2000);
  63. }
  64.  
  65. void loop() {
  66. //Encoder Reset
  67. for (int i = 0; i <= 1; i++)
  68. {
  69. rotating[i] = true;
  70. if (encoderPos[i] != 0)
  71. {
  72. if (i == 0) Mouse.move(encoderPos[i], 0, 0);
  73. if (i == 1) Mouse.move(0, encoderPos[i], 0);
  74.  
  75. encoderPos[i] = 0;
  76. }
  77. }
  78.  
  79. if (analogRead(buttonWhiteL) <= HITBTN) {
  80. Keyboard.press('z');
  81. } else {
  82. Keyboard.release('z');
  83. }
  84.  
  85. if (analogRead(buttonYellowL) <= HITBTN) {
  86. Keyboard.press('s');
  87. } else {
  88. Keyboard.release('s');
  89. }
  90.  
  91. if (analogRead(buttonGreenL) <= HITBTN) {
  92. Keyboard.press('x');
  93. } else {
  94. Keyboard.release('x');
  95. }
  96.  
  97. if (analogRead(buttonBlueL) <= HITBTN) {
  98. Keyboard.press('d');
  99. } else {
  100. Keyboard.release('d');
  101. }
  102.  
  103. if (analogRead(buttonRed) <= HITBTN) {
  104. Keyboard.press('c');
  105. } else {
  106. Keyboard.release('c');
  107. }
  108.  
  109. if (analogRead(buttonBlueR) <= HITBTN) {
  110. Keyboard.press('f');
  111. } else {
  112. Keyboard.release('f');
  113. }
  114.  
  115. if (analogRead(buttonGreenR) <= HITBTN) {
  116. Keyboard.press('v');
  117. } else {
  118. Keyboard.release('v');
  119. }
  120.  
  121. if (analogRead(buttonYellowR) <= HITBTN) {
  122. Keyboard.press('g');
  123. } else {
  124. Keyboard.release('g');
  125. }
  126.  
  127. if (analogRead(buttonWhiteR) <= HITBTN) {
  128. Keyboard.press('b');
  129. } else {
  130. Keyboard.release('b');
  131. }
  132.  
  133. delay(DELAY);
  134. }
  135.  
  136. void doEncoderA()
  137. {
  138.  
  139. if( digitalRead(encoderPinA) != A_set )
  140. {
  141. A_set = !A_set;
  142.  
  143. if ( A_set && !B_set )
  144. encoderPos[0] += 1;
  145.  
  146. rotating[0] = false;
  147. }
  148. }
  149.  
  150. void doEncoderB()
  151. {
  152.  
  153. if( digitalRead(encoderPinB) != B_set ) {
  154. B_set = !B_set;
  155.  
  156. if( B_set && !A_set )
  157. encoderPos[0] -= 1;
  158.  
  159. rotating[0] = false;
  160. }
  161. }
  162.  
  163. void doEncoderC()
  164. {
  165. if( digitalRead(encoderPinC) != C_set )
  166. {
  167. C_set = !C_set;
  168.  
  169. if ( C_set && !D_set )
  170. encoderPos[1] -= 1;
  171.  
  172. rotating[1] = false;
  173. }
  174. }
  175.  
  176. void doEncoderD()
  177. {
  178. if( digitalRead(encoderPinD) != D_set ) {
  179. D_set = !D_set;
  180.  
  181. if( D_set && !C_set )
  182. encoderPos[1] += 1;
  183.  
  184. rotating[1] = false;
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement