Rotary Encoder AS5304

Hi guys,

I am trying to read the rotary encoder AS5304 and began with the source of http://playground.arduino.cc/Main/RotaryEncoders

The following is my code but Rotation variable does not change. I can see the Encoder responses correctly.
Encoder A and B gives me 1 and 0, respectively. However, IGUS_1_Rot is always 0.

I am using Arduino Mega 2560 and connected encoder pins to 35, 36 and 37, respectively. Therefore I use PinChangeInt. I dont think my problem is caused by PinChangeInt.

Looking forward to your help…

#include <Stepper.h>
#include <PinChangeInt.h>

#define IGUS_1_Encoder_Index  35
#define IGUS_1_Encoder_A      36
#define IGUS_1_Encoder_B      37

//Stepper IGUS(1, STEPPER_STEP_PIN, STEPPER_DIR_PIN);
Stepper IGUS_1(STEPS_1, STEPPER_1_STEP_PIN, STEPPER_1_DIR_PIN);

int XVal = 0;
int YVal = 0;
int ZVal = 0;
int X_prev = 0;
int Y_prev = 0;
int Z_prev = 0;

volatile unsigned int IGUS_1_Rot = 0;

void setup() {

  Serial.begin(57600);
  pinMode(IGUS_1_Encoder_Index, INPUT); digitalWrite(IGUS_1_Encoder_Index, HIGH);
  pinMode(IGUS_1_Encoder_A, INPUT); digitalWrite(IGUS_1_Encoder_A, HIGH);
  pinMode(IGUS_1_Encoder_B, INPUT); digitalWrite(IGUS_1_Encoder_B, HIGH);

  // External Interrupts to Encoders
  PCintPort::attachInterrupt(IGUS_1_Encoder_A, Update_IGUS_1, CHANGE);
  
  // set the speed of the motors
  IGUS_1.setSpeed(120);
}

void loop() {
  // Read from Joystick
  XVal = analogRead(XAxis);
  YVal = analogRead(YAxis);
  ZVal = analogRead(ZAxis);

  int i1_en_index = digitalRead(IGUS_1_Encoder_Index);
  int i1_en_A = digitalRead(IGUS_1_Encoder_A);
  int i1_en_B = digitalRead(IGUS_1_Encoder_B);

  // IGUS_1
  if (ZVal < 400){
    IGUS_1.step(100);
  }
  else if (ZVal > 700){
    IGUS_1.step(-100);
  }
  

//  Serial.print("H1: ");Serial.print(i1_hall);Serial.print(" | ");
//  Serial.print("H1_Eindex: ");Serial.print(i1_en_index);Serial.print(" | ");
  Serial.print("H1_EA: ");Serial.print(i1_en_A);Serial.print(" | ");
  Serial.print("H1_EB: ");Serial.print(i1_en_B);Serial.print(" | ");
  Serial.println(IGUS_1_Rot); 

}

void Update_IGUS_1() {

  if (digitalRead(IGUS_1_Encoder_A) == HIGH) {   // found a low-to-high on channel A
    if (digitalRead(IGUS_1_Encoder_B) == LOW) {  // check channel B
      IGUS_1_Rot--;         // CCW
    } 
    else {
      IGUS_1_Rot++;         // CW
    }
  }
  else                                        // found a high-to-low on channel A
  { 
    if (digitalRead(IGUS_1_Encoder_B) == LOW) {   // check channel B
      IGUS_1_Rot++;          // CW
    } 
    else {
      IGUS_1_Rot--;          // CCW
    }
  }
}

The only pins that have pinchange interrupts on the Mega are 0, 10..15, 50..53 and A8..A15

There are only 24 possible pinchange pins and on Mega chips its ports B, J, K (with a couple of
exceptions).

Thank you so much...