Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. const unsigned long ROTARY_DEBOUNCE_TIME = 100; // milliseconds
  2. const byte Encoder_A_Pin = 8; // PB0, pin 12 (TQFP) on board
  3. const byte Encoder_B_Pin = 9; // PB1, pin 13 (TQFP) on board
  4.  
  5. // handle pin change interrupt for D8 to D13 here
  6. ISR (PCINT0_vect)
  7. {
  8. static byte pinA, pinB;
  9. static boolean ready;
  10. static unsigned long lastFiredTime;
  11.  
  12. byte newPinA = digitalRead (Encoder_A_Pin);
  13. byte newPinB = digitalRead (Encoder_B_Pin);
  14.  
  15. if (pinA == newPinA &&
  16. pinB == newPinB)
  17. return; // spurious interrupt
  18.  
  19. // so we only record a turn on both the same (HH or LL)
  20.  
  21. // Forward is: LH/HH or HL/LL
  22. // Reverse is: HL/HH or LH/LL
  23.  
  24. if (newPinA == newPinB)
  25. {
  26. if (ready)
  27. {
  28.  
  29. if (millis () - lastFiredTime >= ROTARY_DEBOUNCE_TIME)
  30. {
  31. if (newPinA == HIGH) // must be HH now
  32. {
  33. if (pinA == LOW)
  34. fileNumber ++;
  35. else
  36. fileNumber --;
  37. }
  38. else
  39. { // must be LL now
  40. if (pinA == LOW)
  41. fileNumber --;
  42. else
  43. fileNumber ++;
  44. }
  45. if (fileNumber > MAX_FILE_NUMBER)
  46. fileNumber = 0;
  47. else if (fileNumber < 0)
  48. fileNumber = MAX_FILE_NUMBER;
  49. lastFiredTime = millis ();
  50. fired = true;
  51. }
  52.  
  53. ready = false;
  54. } // end of being ready
  55. } // end of completed click
  56. else
  57. ready = true;
  58.  
  59. pinA = newPinA;
  60. pinB = newPinB;
  61.  
  62. } // end of PCINT2_vect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement