nevetsokyeron
Member
Starting with the Basic encoder example, I want to take an encoder value and map that to a 0-127 MIDI controller value.
Ideally - when I get to 127 it should continue to give me 127 and at zero continue to give me 0.
I've got some code which gives me that, however I get alternating values (+/- 1) for almost every turn of the encoder. Most obvious at the zero end where if I keep turning the encoder I get alternating 0 and 1's
Is there something I can do to avoid this jitter?
prints:
0
1
0
1
0
1
0
1
....
119
120
119
120
121
122
123
124
125
126
125
126
127
126
127
Ideally - when I get to 127 it should continue to give me 127 and at zero continue to give me 0.
I've got some code which gives me that, however I get alternating values (+/- 1) for almost every turn of the encoder. Most obvious at the zero end where if I keep turning the encoder I get alternating 0 and 1's
Is there something I can do to avoid this jitter?
HTML:
#include <Encoder.h>
Encoder myEnc(20, 21);
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if(newPosition > 127) {
myEnc.write(127);
} else if(newPosition < 0) {
myEnc.write(0);
}
newPosition = constrain(newPosition, 0, 127);
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
0
1
0
1
0
1
0
1
....
119
120
119
120
121
122
123
124
125
126
125
126
127
126
127
Last edited: