Encoder
Encoder copied to clipboard
Interrupt issue with read function with Due but not Mega
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;
}