I have a basic, bare rotary encoder attached to an Adafruit Feather M4 Express (SAMD51). I have tried the regular and interrupt versions of both the Encoder.h library and the RotaryEncoder.h libraries to limited or no effect.
First, the Encoder.h library is the only one that has given an output but not what I expected. This is the modified Basic.ino (interrupts enables I believe).
#include <Encoder.h>
// Change these two 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 myEnc(9, 11);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
Expecting a single number either incrementing or decrementing or some variation based on direction, instead I get
1
0
No matter which way i turn it. sometimes i'll get this repeated 2,4,6 times with on click of the knob. This is true of both the Basic and NoInterrupt examples.
When using the RotaryEncoder.h library, I get no output. Here is the modified version of the InterruptEncoder example.
#include <Arduino.h>
#include <RotaryEncoder.h>
#define PIN_IN1 9
#define PIN_IN2 11
// A pointer to the dynamic created rotary encoder instance.
// This will be done in setup()
RotaryEncoder *encoder = nullptr;
void checkPosition()
{
encoder->tick(); // just call tick() to check the state.
}
void setup()
{
Serial.begin(115200);
while (!Serial)
;
Serial.println("InterruptRotator example for the RotaryEncoder library.");
// setup the rotary encoder functionality
// use FOUR3 mode when PIN_IN1, PIN_IN2 signals are always HIGH in latch position.
// encoder = new RotaryEncoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3);
// use FOUR0 mode when PIN_IN1, PIN_IN2 signals are always LOW in latch position.
// encoder = new RotaryEncoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR0);
// use TWO03 mode when PIN_IN1, PIN_IN2 signals are both LOW or HIGH in latch position.
encoder = new RotaryEncoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
// register interrupt routine
attachInterrupt(digitalPinToInterrupt(PIN_IN1), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_IN2), checkPosition, CHANGE);
} // setup()
// Read the current position of the encoder and print out when changed.
void loop()
{
static int pos = 0;
encoder->tick(); // just call tick() to check the state.
int newPos = encoder->getPosition();
if (pos != newPos) {
Serial.print("pos:");
Serial.print(newPos);
Serial.print(" dir:");
Serial.println((int)(encoder->getDirection()));
pos = newPos;
}
}
There is nothing printed to Serial with this, nor is there anything with the LimitedRotator or SimplePollRotator examples.
Really at a loss here. Does it have something to do with the M4 architecture? I can't change the hardware at this point. Ideally I would like to stay in Arduino code and not port over to CircuitPython.
Any suggestions?