Advertisement
ProjectAnima

SDVX

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