Interrupt + encoder + DUE

Hello,
I'm experiencing a strange behavior whit Arduino Due,
Running this simple code I'm returning the encoder with HALF RESOLUTION,

#define encoder0PinA  20
#define encoder0PinB  21
volatile long encoder0Pos = 0;

void setup() { 

  pinMode(encoder0PinA, INPUT); 
  pinMode(encoder0PinB, INPUT); 
  
    attachInterrupt(20, doEncoderMotor0, CHANGE);  
  attachInterrupt(21, doEncoderMotor1, CHANGE);  
  
  Serial.begin (9600);
  Serial.println("start");                // a personal quirk

} 



void loop(){
  
     Serial.println ( encoder0Pos );
     }



void doEncoderMotor0(){
  if (digitalRead(encoder0PinA) == HIGH) {   // found a low-to-high on channel A
    if (digitalRead(encoder0PinB) == LOW) {  // check channel B to see which way
                                             // encoder is turning
      encoder0Pos = encoder0Pos - 1;         // CCW
    } 
    else {
      encoder0Pos = encoder0Pos + 1;         // CW
    }
  }
  else                                        // found a high-to-low on channel A
  { 
    if (digitalRead(encoder0PinB) == LOW) {   // check channel B to see which way
                                              // encoder is turning  
      encoder0Pos = encoder0Pos + 1;          // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;          // CCW
    }

  }
 
}


void doEncoderMotor1(){

  // look for a low-to-high on channel B
  if (digitalRead(encoder0PinB) == HIGH) {   

    // check channel A to see which way encoder is turning
    if (digitalRead(encoder0PinA) == LOW) {  
      encoder0Pos = encoder0Pos + 1;         // CCW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;         // CW
    }
  }

  // Look for a high-to-low on channel B

  else { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(encoder0PinA) == HIGH) {   
      encoder0Pos = encoder0Pos + 1;          // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;          // CCW
    }
  }

}

was fine with Arduino MEGA..
Any Theory? I'm getting mad...
Thank you

Luca

I had some troubles with the new Due and interrupts on several pins but this patch has saved me: Arduino Forum

Hope it helps! :wink: