Hello,
I’m using an Arduino Due for a school project, and I’m attempting to use two of the Rotary Encoders purchased from this site (COM-09117) and I’m using an “Encoder” library found on the Arduino playground http://www.pjrc.com/teensy/td_libs_Encoder.html and I cannot get it to function the way it did on a Arduino Leonardo.
The Leonardo, when using the two encoders, would get the correct number of clicks when the encoder was turned for both instances of the encoders created, but the Due misinterprets the turns completely and it doesn’t count up or down based on the direction the encoder is turned. When the wires are bumped, the code registers a bunch of hits in the serial monitor window.
Here is the sample code for using two encoders that I’m testing out for the encoder library.
/* Encoder Library - TwoKnobs Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these pin numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder knobLeft(32, 22);
Encoder knobRight(52, 53);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("TwoKnobs Encoder Test:");
}
long positionLeft = -999;
long positionRight = -999;
void loop() {
long newLeft, newRight;
newLeft = knobLeft.read();
newRight = knobRight.read();
if (newLeft != positionLeft || newRight != positionRight) {
Serial.print("Left = ");
Serial.print(newLeft);
Serial.print(", Right = ");
Serial.print(newRight);
Serial.println();
positionLeft = newLeft;
positionRight = newRight;
}
// if a character is sent from the serial monitor,
// reset both back to zero.
if (Serial.available()) {
Serial.read();
Serial.println("Reset both knobs to zero");
knobLeft.write(0);
knobRight.write(0);
}
}
The header file (where all the code is located) and the .cpp file are all on the website with the link provided.
I have also tried several other implementations from the Arduino Rotary Encoder page ```
http://playground.arduino.cc/Main/RotaryEncoders
I completely understand that the MCU is much faster on the Due than on the Leonardo, but the library has "support" for the Due, at least on the interrupts, so I'm not sure why there is an issue.
Either way, I wanted to see if anyone had any experience with the Due and a rotary encoder, or any advice to give me, as I have primarily only worked with 8-bit MCU's in the past.
Thank you!
- John