Pages: [1]   Go Down
Author Topic: Interrupt + encoder + DUE  (Read 991 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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
Logged

Bologna, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley-wink
Logged

Pages: [1]   Go Up
Jump to: