Using the example in the PJRC encoder library, I'm trying to constrain my encoder values between 0 - 127. There's conflict between how the encoder wants to increment and how the counter acts. Using the "TwoKnobs Example", with only one encoder, My rotary encoder wants to count by 4 with each 'click' (each time both sensors are passed). I've tried several variations of bit-shifting "newLeft", as well as dividing it by four.
All of these result in things like:
- Increments by 1 per click (desired) but only counts to 64.
- Counts to 127 and starts over at zero (desired), but increments/decrements by 2.
How do I increment by one each time a 'click' is felt on my encoder (reading both sensors once), while at the same time having the values constrained to 0 - 127? You'll notice I tried to add another variable altogether in an attempt to constrain the value, which hasn't been successful either. Thanks.
All of these result in things like:
- Increments by 1 per click (desired) but only counts to 64.
- Counts to 127 and starts over at zero (desired), but increments/decrements by 2.
How do I increment by one each time a 'click' is felt on my encoder (reading both sensors once), while at the same time having the values constrained to 0 - 127? You'll notice I tried to add another variable altogether in an attempt to constrain the value, which hasn't been successful either. Thanks.
Code:
#include <Encoder.h>
Encoder knobLeft(25, 26);
void setup() {
Serial.begin(9600);
Serial.println("TwoKnobs Encoder Test:");
}
byte positionLeft = 0;
void loop() {
byte newLeft;
byte finalVal;
newLeft = knobLeft.read();
if ((newLeft >> 1) > 127) {
newLeft = 0;
}
if ((newLeft >> 1) < 0) {
newLeft = 127;
}
finalVal = (newLeft >> 1);
if (newLeft != positionLeft) {
positionLeft = newLeft;
Serial.print("Left = ");
Serial.print(finalVal);
Serial.println();
}
}