Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interrupt issue with read function with Due but not Mega #75

Open
jacobdmoore opened this issue Feb 2, 2022 · 0 comments
Open

Interrupt issue with read function with Due but not Mega #75

jacobdmoore opened this issue Feb 2, 2022 · 0 comments

Comments

@jacobdmoore
Copy link

Description

Having an issue with basic encoder functionality using Arduino Due specifically (code works on Arduino Mega no problem). It can be worked around by not using interrupts. However, it would be good for my application to be able to use interrupts.

Steps To Reproduce Problem

I used the attached code to perform the read function from this library. This works no problem on the Arduino Mega. However, on the Arduino Due, the read function works only when I use #define ENCODER_DO_NOT_USE_INTERRUPTS. When that line is not included, the CLK and DT values update as expected for the Due, but myEnc.read() returns a constant 0.

Hardware & Software

Arduino Mega 2560 R3 and Arduino Due
Arduino IDE 2.0.0-rc2
Encoder.h 1.4.2, Arduino SAM Boards (32-bits ARM Cortex-M3) 1.6.12 for Due, and Arduino AVR Boards 1.8.4 for Mega
Windows 10

Arduino Sketch

#define ENCODER_DO_NOT_USE_INTERRUPTS // this line is required for Due to function properly, can be disabled for Mega
#include <Encoder.h>

// Encoder library available at https://www.pjrc.com/teensy/td_libs_Encoder.html
// This should automatically set these pins to interrupt mode
#define ENCODER_CLK_PIN 19
#define ENCODER_DT_PIN 18
Encoder myEnc(ENCODER_DT_PIN, ENCODER_CLK_PIN);

int previous_encoder_pos = 0;
int current_encoder_pos = 0;

void setup(void) {
  Serial.begin(9600);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Encoder test!");
}

void loop() { 

  current_encoder_pos = myEnc.read();

  if (current_encoder_pos > previous_encoder_pos){
    Serial.print("DT: ");
    Serial.println(digitalRead(ENCODER_DT_PIN));
    Serial.print("CLK: ");
    Serial.println(digitalRead(ENCODER_CLK_PIN));
    Serial.print("current_encoder_pos = ");
    Serial.println(current_encoder_pos);
  }
  else if (current_encoder_pos < previous_encoder_pos){
    Serial.print("DT: ");
    Serial.println(digitalRead(ENCODER_DT_PIN));
    Serial.print("CLK: ");
    Serial.println(digitalRead(ENCODER_CLK_PIN));
    Serial.print("current_encoder_pos = ");
    Serial.println(current_encoder_pos);
  }

  previous_encoder_pos = current_encoder_pos;

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant