Advertisement
Guest User

code

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