Encoders and MIDI output - jittery alternating values

Status
Not open for further replies.
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?
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);
  }
}
prints:
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:
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?
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);
  }
}

prints:
0
1
0
1
0
1
0
1
....
119
120
119
120
121
122
123
124
125
126
125
126
127
126
127

I don’t know what your circuit looks like but you may need to filter the outputs of your encoders, this seems to be a good example of a filter circuit for encoders.
2DC00E6C-A018-455B-BC46-E1684104C6F8.jpg

However, since the jitter seems to be only one value off you could instead map the encoder as 0-255 and then bit shift it to the right before writing your new position variable. This of course makes you have to turn the encoder twice as far for the same value, so there is that. Is the jitter constant where if you don’t touch it it still jitters oris it only while moving the encoder?
 
Status
Not open for further replies.
Back
Top