You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been implementing a project in kaluma that uses a rotary encoder and noted it was quite noisy. While obviously part of the problem is the low quality encoders I procured, a better algorithm would compensate quite a lot.
Based on some documentation I found on the web (the code was for arduino and compatibles), I wrote a replacement onChange event. I tested the code and the noise reduction is dramatic (0 false rotations), while only experiencing some misses (about 1 of every 15 "clicks" in my crappy encoder)
(the onChange event follows)
// contains the last transition: 2 positions of both pins in binary form (clk previous, dt previous, clk current, dt current)
this._prevNextCode = 0;
// contains the last 4 valid transitions
this._encData = 0;
const onChange = () => {
// of the 16 combinations for prevNextCode only 8 are valid.
const valid_positions = [false,true,true,false,true,false,false,true,true,false,false,true,false,true,true,false];
// Store the digital read in prevNextCode and trim it
this._prevNextCode <<=2;
if (digitalRead(clkPin)) this._prevNextCode |= 0x02;
if (digitalRead(dtPin)) this._prevNextCode |= 0x01;
this._prevNextCode &= 0x0f;`
// Continue only if the current reading is a valid combination
if (valid_positions[this._prevNextCode & 0x0f]) {
// store the current combination
this._encData <<= 4;
this._encData |= this._prevNextCode;
// All rotation cycles either end in the transitions 00>01 01>11 or 00>10 10>11
// if the encoded data matches, we emit a rotate event
if ((this._encData&0xff) === 0x2b) this.emit("rotate", 1);
if ((this._encData&0xff) === 0x17) this.emit("rotate", -1);
// we trim the result to save the last 4 transitions
this._encData &= 0xffff;
}
}
I was wondering if it would be better to submit it as a pull request or make a separate module, in case someone wants to use it.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've been implementing a project in kaluma that uses a rotary encoder and noted it was quite noisy. While obviously part of the problem is the low quality encoders I procured, a better algorithm would compensate quite a lot.
Based on some documentation I found on the web (the code was for arduino and compatibles), I wrote a replacement onChange event. I tested the code and the noise reduction is dramatic (0 false rotations), while only experiencing some misses (about 1 of every 15 "clicks" in my crappy encoder)
(the onChange event follows)
I was wondering if it would be better to submit it as a pull request or make a separate module, in case someone wants to use it.
Beta Was this translation helpful? Give feedback.
All reactions