Sorry for the late reply.
Do you mean this code by @jeremydouglass and @raron ?
I tested just with one knob and didn’t see any flickering.
// Rotary Encoder demo
// Code by @raron and @kerremydouglass
HwRotaryEncoder enc;
float scrollWheel = 0.0;
void setup() {
fullScreen();
ellipseMode(RADIUS);
enc = new HwRotaryEncoder(width/2, height/2, width/3, 8);
}
void draw() {
background(0, 0, 80);
fill(0);
enc.getDirection();
enc.update();
enc.display();
}
class HwRotaryEncoder {
PVector center;
PVector startDragPos;
PVector currentPos;
float encSize, slice;
float rotation, oldRotation;
int quadsteps, quadIndex, oldQuadIndex;
int direction;
boolean encA, encB, oldEncA, oldEncB;
boolean active, mouseButtonWasPressed;
color knobColor;
HwRotaryEncoder(float w, float h, float encSize, int stepsPerRev) {
this.center = new PVector(w, h);
this.startDragPos = new PVector(0, w);
this.currentPos = new PVector(0, w);
this.encSize = encSize;
this.quadsteps = stepsPerRev*4; // internal counts per step (quadrature encoder)
this.slice = TWO_PI/this.quadsteps;
this.direction = 0;
this.rotation = 0;
this.oldRotation = 0;
this.oldEncA = false;
this.oldEncB = false;
this.active = false;
}
boolean mouseInside() {
if (currentPos.mag() < encSize) return true;
return false;
}
void update() {
currentPos = new PVector(mouseX, mouseY).sub(center);
if (mousePressed == false) {
// a mouse button is not pressed
if (active) {
active = false;
oldRotation = rotation;
}
mouseButtonWasPressed = false;
} else {
// a mouse button is pressed
if (mouseButtonWasPressed == false) {
mouseButtonWasPressed = true;
if (mouseInside()) {
active = true;
startDragPos = new PVector(mouseX, mouseY).sub(center);
oldEncA = false;
oldEncB = false;
direction = 0;
}
}
}
// Mouse drag rotation
// If moving mouse fast, will probably skip steps or count backwards
if (active) {
rotation = currentPos.heading() - startDragPos.heading() + oldRotation;
if (rotation<0) rotation += TWO_PI;
if (rotation>TWO_PI) rotation -= TWO_PI;
int quadIndex = (int)((float)quadsteps * (rotation/TWO_PI));
if (oldQuadIndex != quadIndex) {
// Make rotary encoder quadrature pattern
int pa = (quadIndex/2)%2;
int pb = ((quadIndex+1)/2)%2;
if (pa == 1) encA = true;
else encA = false;
if (pb == 1) encB = true;
else encB = false;
// Count when transitioning pattern edges
if (encB & (encA ^ oldEncA)) {
if (encA) direction = 1;
else direction = -1;
}
if (!encB & (encA ^ oldEncA)) {
if (encA) direction = -1;
else direction = 1;
}
if (encA & (encB ^ oldEncB)) {
if (encB) direction = -1;
else direction = 1;
}
if (!encA & (encB ^ oldEncB)) {
if (encB) direction = 1;
else direction = -1;
}
oldEncA = encA;
oldEncB = encB;
oldQuadIndex = quadIndex;
}
}
}
int getDirection() {
int temp = direction;
direction = 0;
return temp;
}
void display() {
if (mouseInside() || active) knobColor = color(210);
else knobColor = color(192);
pushMatrix();
pushStyle();
translate(center.x, center.y);
stroke(255);
for (int i=0; i < quadsteps; i++) {
if (i == 0) strokeWeight(6);
else strokeWeight(3);
line( encSize*0.9, 0, encSize, 0 );
rotate(slice);
}
fill(knobColor);
ellipse(0, 0, encSize*6/8, encSize*6/8);
rotate(rotation);
stroke(0);
strokeWeight(6);
line( 0, 0, encSize*6/8, 0 );
popMatrix();
popStyle();
}
}