Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. void setup() {
  2. //Use this setup code for 12 ticks/revolution
  3. attachInterrupt(digitalPinToInterrupt(rotaryPinA), encode, FALLING);
  4.  
  5. /*
  6. Use this setup code for 24 ticks/revolution
  7. (trigger interrupt on rising and falling edge)
  8. attachInterrupt(digitalPinToInterrupt(rotaryPinA), encode, CHANGE);
  9. */
  10.  
  11. /*
  12. Use this setup code for 48 ticks/revolution (trigger interrupts on rising and falling edge of either pin)
  13. attachInterrupt(digitalPinToInterrupt(rotaryPinA), encode, CHANGE);
  14. attachInterrupt(digitalPinToInterrupt(rotaryPinB), encodePinB, CHANGE);
  15. */
  16.  
  17. //The rest of your setup code...
  18. }
  19.  
  20. void encode() {
  21. rotaryValueChanged = true;
  22. //Read the PinA & PinB (Digital Pins 2 & 3) using port register PINE, 4th and 5th bit
  23. //Fast equivalent to pinAState = digitalRead(rotaryPinA) == LOW
  24. bool pinAState = (PINE & (1 << 4)) == 0;
  25. bool pinBState = (PINE & (1 << 5)) == 0;
  26. rotaryValue += (pinAState == pinBState) ? -1 : 1;
  27. }
  28.  
  29. void encodePinB() {
  30. rotaryValueChanged = true;
  31. //Read the PinA & PinB (Digital Pins 2 & 3) using port register PINE, 4th and 5th bit
  32. bool pinAState = (PINE & (1 << 4)) == 0;
  33. bool pinBState = (PINE & (1 << 5)) == 0;
  34. rotaryValue += (pinAState != pinBState) ? -1 : 1;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement