Guest User

rotaryEncoder_interrupt

a guest
Jun 2nd, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
  4. static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
  5. volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
  6. volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
  7. volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
  8. volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
  9. volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent
  10.  
  11. void setup() {
  12.   pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  13.   pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  14.   attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  15.   attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  16.   Serial.begin(115200); // start the serial monitor link
  17. }
  18.  
  19. void PinA(){
  20.   cli(); //stop interrupts happening before we read pin values
  21.   reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  22.   if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
  23.     encoderPos --; //decrement the encoder's position count
  24.     bFlag = 0; //reset flags for the next turn
  25.     aFlag = 0; //reset flags for the next turn
  26.   }
  27.   else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  28.   sei(); //restart interrupts
  29. }
  30.  
  31. void PinB(){
  32.   cli(); //stop interrupts happening before we read pin values
  33.   reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  34.   if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
  35.     encoderPos ++; //increment the encoder's position count
  36.     bFlag = 0; //reset flags for the next turn
  37.     aFlag = 0; //reset flags for the next turn
  38.   }
  39.   else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  40.   sei(); //restart interrupts
  41. }
  42.  
  43. void loop(){
  44.   if(oldEncPos != encoderPos) {
  45.     Serial.println(encoderPos);
  46.     oldEncPos = encoderPos;
  47.   }
  48. }
Add Comment
Please, Sign In to add comment