Best way to read the encoder?

I’m using the code below. It sometimes it misses changes to the encoder. I was guess that using Delay() would improves performance? Is this not a good idea, I’m guessing this is the reason the encoder is not as responsive as I’d like it to be.

while (1) {
    synth.UpdateDisplay();
    synth.UpdateControls();
    daisy::System::Delay(5);
}

I’m reading the encoder with:

void ByteShift::UpdateControls() {
    patch->ProcessAnalogControls(); // Read knob values
    patch->ProcessDigitalControls(); // Read encoder

    // Detect encoder rotation
    int increment = patch->encoder.Increment();
    if (increment != 0) {
        encoderCount += increment;
    }

    // Detect encoder press
    if (patch->encoder.RisingEdge()) {
        encoderCount = 0;  // Reset on press (for debugging)
    }
}

Have you looked at example code for Pod, Patch?

There is indeed an encoder class in libDaisy. But getting ‘perfect’ responses can sometimes be tricky.
Some encoders give 4 pulse per tick. Some latch on the 3rd. Some on 3rd and 1st, etc…
Also note that one should use a state machine to keep the encoder state current, which needs to be run often at small intervals, then reading the current position could be at a smaller interval.
This is why some encoder classes have a ‘Tick’ function.

Thanks for the info, I’m experimenting with the encoder. Seems like it might be best to poll the analog controls less often, and poll the digital controls, like the encoder more often.

My reasoning, is the analog controls take more processing, and are less affected by lag. While the digital controls are the opposite?

I had exactly the same problem. In my case, the cause was UpdateDisplay taking too much time. It would be helpful to measure how much time that function call takes and go from there.

UpdateDisplay() should only be called when necessary.

Would checking the digital encoder every cycle, while reading analog controls and updating the display every 50ms be a reasonable starting place?

Just thinking out loud, looking for ideas from someone with more experience.

Updating display every 50 ms is fine (20 FPS). Checking the encoder every cycle might not be needed, maybe every ms, couple of ms? as long as the state machine remains valid.