SHARE
TWEET

encoder test working 1.6.2020

Zaganu Jun 1st, 2020 804 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
  2. static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
  3. 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
  4. 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)
  5. volatile int 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
  6. volatile int 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)
  7. 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
  8.  
  9. void setup() {
  10.   pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  11.   pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  12.   attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  13.   attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  14.   Serial.begin(9600); // start the serial monitor link
  15. }
  16.  
  17. void PinA(){
  18.   cli(); //stop interrupts happening before we read pin values
  19.   reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  20.   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
  21.     encoderPos --; //decrement the encoder's position count
  22.     bFlag = 0; //reset flags for the next turn
  23.     aFlag = 0; //reset flags for the next turn
  24.   }
  25.   else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  26.   sei(); //restart interrupts
  27. }
  28.  
  29. void PinB(){
  30.   cli(); //stop interrupts happening before we read pin values
  31.   reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  32.   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
  33.     encoderPos ++; //increment the encoder's position count
  34.     bFlag = 0; //reset flags for the next turn
  35.     aFlag = 0; //reset flags for the next turn
  36.   }
  37.   else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  38.   sei(); //restart interrupts
  39. }
  40.  
  41. void loop(){
  42.   if(oldEncPos != encoderPos) {
  43.     Serial.println(encoderPos);
  44.     oldEncPos = encoderPos;
  45.   }
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top