Rotary Encoder- Output to Seven Segment

I realize that their are many different threads about this same topic, but none answer my questions exactly. I have a rotary encoder (https://faculty-web.msoe.edu/prust/EE1910/EE1910Resources/RotaryEncoder_TW-700198.pdf) that I need to count 0-9 by displaying the digits on a seven segment display. The display works, and I can output each number but I am having trouble programming the encoder to count evenly from 0-9. The closest program I’ve tried counts up and down, but jumps in increments of 4.

//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;

volatile int lastEncoded = 0;
volatile long encoderValue = 0;

in lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;

void setup() {
Serial.begin (9600);

pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);

digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on

//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);

}

void loop(){
//Do stuff here

Serial.println(encoderValue);
delay(3000); //just here to slow down the output, and show it will work even during a delay
}

void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit

int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011)
encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000)
encoderValue --;

lastEncoded = encoded; //store this value for next time
}


Any ideas?

What encoder are you using? Are there detents?
Edit: Sorry I missed the link to the encoder in the original post. I see the encoder does have detents.

The interrupts and ISR you are using will count all four available quadrature pulses. Some encoders with detents have the four quadrature counts between detents. If you are getting reliable counts, you can divide by four, or you could modify the isr to only increment or decrement at the detent position. If you want to do that, you will need to determine the last encoded and encoded bit patterns for the detent position with both directions of rotation.

You’re delaying 3 seconds between each println. Chances are you’re dialing another 3 times during that time.

What happens if you print every change like this.

//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;

volatile int lastEncoded = 0;
volatile long encoderValue = 0;

int lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;

void setup() {
  Serial.begin (9600);

  pinMode(encoderPin1, INPUT);
  pinMode(encoderPin2, INPUT);

  digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  digitalWrite(encoderPin2, HIGH); //turn pullup resistor on

  //call updateEncoder() when any high/low changed seen
  //on interrupt 0 (pin 2), or interrupt 1 (pin 3)
  attachInterrupt(0, updateEncoder, CHANGE);
  attachInterrupt(1, updateEncoder, CHANGE);

}

int oldEncValue=0;

void loop(){
  //Do stuff here
  if(oldEncValue != encoderValue)
    {
     Serial.println(encoderValue);
     oldEncValue=encoderValue;
    }
}


void updateEncoder(){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit

  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011)
    encoderValue ++;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000)
    encoderValue --;

  lastEncoded = encoded; //store this value for next time
}