Hi all,
I’m testing an encoder‐reading sketch on a Teensy 4.1 and running into a couple of puzzling behaviors. Any help would be greatly appreciated!
What I’m seeing:
Hardware & setup:
Thanks in advance for any tips.
Serial Print snapshot:
time elapsed, Steps, smallest time elapsed:
153,-1302,22
152,-1301,22
150,-1300,22
155,-1299,22
1590,-1290,22
3,-1289,3
160,-1288,3
1883,-1278,3
3,-1277,3
175,-1276,3
1771,-1266,3
144,-1265,3
1383,-1257,3
53,-1256,3
Here attaching code:
I’m testing an encoder‐reading sketch on a Teensy 4.1 and running into a couple of puzzling behaviors. Any help would be greatly appreciated!
What I’m seeing:
- Step-count “jumps”
- The encoder reading itself (using the Encoder library) is accurate—every physical detent advances or retreats is counted.
- However, in my serial output I sometimes see the printed step count increase (or decrease) by more than one between consecutive prints.
- Spurious delta-time readings
- I’m timing each step event with elapsedMicros() to compute the interval between encoder events.
- Most intervals look reasonable, but every so often elapsedMicros() reports a very small value (e.g. 3 µs), which doesn’t match how fast I’m actually turning the knob.
Hardware & setup:
- Teensy 4.1
- GHS38 solid-shaft incremental rotary encoder (200 PPR; library reports 800 counts/rev)
- TXS0108E 8-channel logic-level converter
- Serial baud rate: 115200
- Why does the printed step count sometimes jump by more than one?
- What can cause those unrealistically small elapsedMicros() intervals, appearing in my timing data?
Thanks in advance for any tips.
Serial Print snapshot:
time elapsed, Steps, smallest time elapsed:
153,-1302,22
152,-1301,22
150,-1300,22
155,-1299,22
1590,-1290,22
3,-1289,3
160,-1288,3
1883,-1278,3
3,-1277,3
175,-1276,3
1771,-1266,3
144,-1265,3
1383,-1257,3
53,-1256,3
Here attaching code:
C++:
#include <Encoder.h>
// Encoder on pins 2 & 3
Encoder myEnc(2, 3);
// Stopwatch for intervals
elapsedMicros timeSinceLastChange;
// State
long oldPosition = -999;
unsigned long fastestDt = 0xFFFFFFFFUL; // max unsigned long
void setup() {
Serial.begin(115200);
// CSV header: dt_us,position,fastest_dt_us
Serial.println(F("dt_us,position,fastest_dt_us"));
}
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
// 1) snapshot and reset interval timer
unsigned long dt = (unsigned long)timeSinceLastChange;
timeSinceLastChange = 0;
// 2) update fastest (minimum) dt
if (dt < fastestDt) {
fastestDt = dt;
}
// 3) update state
oldPosition = newPosition;
// 4) print: current interval, encoder count, and best-so-far
Serial.print(dt);
Serial.print(',');
Serial.print(newPosition);
Serial.print(',');
Serial.println(fastestDt);
}
}