Using a 100PPR encoder with the KD8CEC firmware may cause some issues as the firmware can’t keep up with the pulse train from the encoder.
Sascha Bonnet suggests some simple code modifications make a big difference. He replaced two commands (“millis” to “micros”) in the KD8CEC’s source code and my encoder worked. Here’s his code for you to use as a template for editing the KD8CEC arduino sketch:
-
int enc_read(void) {
-
int result = 0;
-
byte newState;
-
int enc_speed = 0;
-
unsigned long start_at = millis();
-
while (millis() – start_at < 50) { // check if the previous state was stable
-
newState = enc_state(); // Get current state
-
if (newState != enc_prev_state)
-
delayMicroseconds(1);
-
if (enc_state() != newState || newState == enc_prev_state)
-
continue;
-
//these transitions point to the encoder being rotated anti-clockwise
-
if ((enc_prev_state == 0 && newState == 2) ||
-
(enc_prev_state == 2 && newState == 3) ||
-
(enc_prev_state == 3 && newState == 1) ||
-
(enc_prev_state == 1 && newState == 0)){
-
result–;
-
}
-
//these transitions point o the enccoder being rotated clockwise
-
if ((enc_prev_state == 0 && newState == 1) ||
-
(enc_prev_state == 1 && newState == 3) ||
-
(enc_prev_state == 3 && newState == 2) ||
-
(enc_prev_state == 2 && newState == 0)){
-
result++;
-
}
-
enc_prev_state = newState; // Record state for next pulse interpretation
-
enc_speed++;
-
delayMicroseconds(1);
-
}