Go Down

Topic: Interrupt + encoder + DUE (Read 1 time) previous topic - next topic

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

Code: [Select]
#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

Liarco

I had some troubles with the new Due and interrupts on several pins but this patch has saved me: http://arduino.cc/forum/index.php/topic,146430.0.html

Hope it helps! ;)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy