Multiplexer pins causing a lag in rotary encoder

My cheap $3 5 pin rotary encoder breakout usually works by incrementing once per click/turn. Basic stuff. Added it to a code which also included a 4067 multiplexer reading analog potentiometers. It began behaving as if it's not reading the encoder sensors in sync, causing erratic things like getting stuck between values, sometimes going the opposite direction, and only really working when I turn it incredibly slowly, if even.

Meanwhile, the potentiometers attached to the multiplexer work fine.

The section of the code that seems to be causing the trouble is for the multiplexer output pins. When I comment it out, independent of the multiplexer's analog pin for debugging purposes, the encoder goes back to normal behavior. The pins on the board were close so I moved them across them further away, but the lag persists still. You'll see in the code below, the mux pins are in a for loop which continues to the analog read, but the encoder still works with the analog read portion of the loop left in.

is there a way to separate the two in the code so that the encoder is unaffected by the mux pins? Here's the portion of the code that I commented out, which made the encoder work properly again:

 for (int i = 0; i < 16; i++) {
    int channel = i;
    digitalWrite(muxPin0, bitRead(channel, 0));
    digitalWrite(muxPin1, bitRead(channel, 1));
    digitalWrite(muxPin2, bitRead(channel, 2));
    digitalWrite(muxPin3, bitRead(channel, 3));

You do know you can write

for (int channel = 0; channel < 16; channel++) {

...and that we can't see the rest of your code, or your schematic?

I don't think it's possible to read a rotary encoder through a multiplexer. After all, the status of one pin ("data") must be read on a transition of the other one ("clock"). A better way is to connect both wires directly to GPIO pins, and use a library to read the encoder.

See A Library for the Arduino environment for using a rotary encoder as an input.

I've not used rotary encoders for a while but I remember needing to read them once per millisecond for them to be reliable. Introducing a multiplexer between the encoder and the microcontroller is going to make that somewhere between difficult and impossible.

Erik_Baas:
I don't think it's possible to read a rotary encoder through a multiplexer. After all, the status of one pin ("data") must be read on a transition of the other one ("clock").

You can poll a pin status instead of using interrupts. But polling interval should be around 1mSec. So it's possible to use a multiplexer but it's not a trivial task.