// https://wokwi.com/projects/357828229247849473
// https://forum.arduino.cc/t/uno-vs-mega-with-a-rotary-encoder/1095187

/*
// digital pins 2 and 3

# define PINX     PINE
# define maskAB   0x30
# define maskB    0x10
# define maskA    0x20

# define pinA     2
# define pinB     3
*/


// digital pins 18 and 19

# define PINX     PIND
# define maskAB   0x0c
# define maskB    0x04
# define maskA    0x08

# define pinA     19
# define pinB     18

/*
// digital pins 20 and 21

# define PINX     PIND
# define maskAB   0x03
# define maskB    0x01
# define maskA    0x02

# define pinA     21
# define pinB     20
/**/

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
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)
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
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)
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

void setup() {
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)


  attachInterrupt(digitalPinToInterrupt(pinA),PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(digitalPinToInterrupt(pinB),PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)


  Serial.begin(9600); // start the serial monitor link
  Serial.println("-------------rotaryEncoderWorkup_v1.1-------------------");
  Serial.println(encoderPos);
}

void PinA(){ //from interrupt above
  reading = PINX & maskAB; // read all eight pin values then strip away all but pinA and pinB's values
  if(reading == maskAB && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    if  (encoderPos>18){encoderPos=18;}
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == maskB) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
}

void PinB(){  //from interrupt above
  reading = PINX & maskAB; //read all eight pin values then strip away all but pinA and pinB's values
  if (reading == maskAB && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos ++; //increment the encoder's position count
    if (encoderPos>18){encoderPos=0;}
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == maskA) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
}

void loop(){
  if(oldEncPos != encoderPos) {
    Serial.println(encoderPos);
    oldEncPos = encoderPos;
  }
}