Using only one knob from the "TwoKnobs Encoder Test" from the PJRC encoder library to read a cheap quadrature encoder that I picked up. Got help on this forum a few months ago to constrain the value to the 0 - 127 midi standard. Of all of the interrupt-based encoder sketches that I was able to find online, this one came the closest to what I'm trying to do, which is simply count up and down by one-per-click.
The one problem with this code is that when I turn it a certain speed (not very fast at all), the value will most often lurch forward or backward by as much as 40-60. This behavior is inconsistent because sometimes it counts normally, though that is rare. Is there something about this code that I need to change in order to prevent this behavior? I'm not looking for laser accurate readings e.g. I don't mind if it doesn't read every click if I twist it quickly, what's most important for me is that the count remains sequential instead of jumping uncontrollably.
If there's a better solution for a cheap rotary encoder that increments/decrements once per click, and is relatively understandable for someone who doesn't specialize in coding, I'm all ears. Thanks!
The one problem with this code is that when I turn it a certain speed (not very fast at all), the value will most often lurch forward or backward by as much as 40-60. This behavior is inconsistent because sometimes it counts normally, though that is rare. Is there something about this code that I need to change in order to prevent this behavior? I'm not looking for laser accurate readings e.g. I don't mind if it doesn't read every click if I twist it quickly, what's most important for me is that the count remains sequential instead of jumping uncontrollably.
If there's a better solution for a cheap rotary encoder that increments/decrements once per click, and is relatively understandable for someone who doesn't specialize in coding, I'm all ears. Thanks!
Code:
#include <Encoder.h>
Encoder knobLeft(40, 39);
void setup() {
Serial.begin(9600);
Serial.println("TwoKnobs Encoder Test:");
}
int positionLeft = 0;
void loop() {
byte newLeft;
byte finalVal;
newLeft = knobLeft.read() / 2;
finalVal = newLeft >> 1;
if (newLeft != positionLeft) {
positionLeft = newLeft;
Serial.print("Left = ");
Serial.print(finalVal);
Serial.println();
}
}