analogRead() and digitalRead() on the same pin in one sketch

I am trying to use an UNO R4 wifi with a CD74HC4067 analog multiplexer to read the values of a bunch inputs on one pin. Some of the inputs are potentiometers and some switches, and the mux connects them all to one pin, so my thought was to connect the mux to an analog pin and do digitalReads for the switches and analogReads for the pots. The 4 select lines of the multiplexer are connected to digital pins, and the common I/O pin to A5. There is a 10k external pullup on A5, and the pots/switches go through the mux from A5 to ground. My code is below:

//inputs
#define INPUT_MUX_PIN A5
#define S0 9
#define S1 10
#define S2 11
#define S3 12
uint16_t channels[16];

void setupMux(){ //setup the pins for the multiplexer
    pinMode(INPUT_MUX_PIN, INPUT);
    pinMode(S0, OUTPUT);
    pinMode(S1, OUTPUT);
    pinMode(S2, OUTPUT);
    pinMode(S3, OUTPUT);
}

void selectChannel(uint8_t channel){ //write mux selector pins to connect a channel
    if(channel < 16){
        digitalWrite(S0, (channel & 0b0001));
        digitalWrite(S1, (channel & 0b0010));
        digitalWrite(S2, (channel & 0b0100));
        digitalWrite(S3, (channel & 0b1000));
    }
}

uint16_t readInput(uint8_t channel = 0, uint8_t digital_read = 0){
    uint16_t read_val = 1;
    if(channel < 16){
        selectChannel(channel);
        delayMicroseconds(1); //allow for switching time, very conservative estimate
        if(digital_read){ 
            read_val = digitalRead(INPUT_MUX_PIN);
        }
        else{
            read_val = (uint16_t)analogRead(INPUT_MUX_PIN);
        }
        channels[channel] = read_val;
    }
    return read_val;
}

void setup(){
    //setup serial
    Serial.begin(9600);
    //setup ADC and mux
    setupMux();
}

void loop(){
    //read and print the pin values
    delay(500);
    Serial.println();
    Serial.println(readInput(15));
    Serial.println(readInput(15,1));
    Serial.println(readInput(14));
    Serial.println(readInput(14,1));
}

When I run this code, the analogReads return correct values, but the digitalReads always return 0. If I delete the analogRead calls and only do digitalReads, it returns correct values. Any idea what is wrong? I know I could just always do analogReads and shift the values to get a digital 1/0, but I want to understand what is not working.

Why not just use analogRead() and test the value returned to interpret is as a digital value based on the reading ?

To do a digital read you need to reset the mode to digital input. The analog read will set the mode to analog input. As @UKHeliBob suggested just do analog read for the digital inputs and treat "low" values as 0 and "high" values as 1.

From the Wifi's pins_arduino.h header:

#define PIN_A0   (14u)
#define PIN_A1   (15u)
#define PIN_A2   (16u)
#define PIN_A3   (17u)
#define PIN_A4   (18u)
#define PIN_A5   (19u)
.
.
.
// Digital pins
// -----------
#define PIN_D0   (0u)
#define PIN_D1   (1u)
#define PIN_D2   (2u)
#define PIN_D3   (3u)
#define PIN_D4   (4u)
#define PIN_D5   (5u)
#define PIN_D6   (6u)
#define PIN_D7   (7u)
#define PIN_D8   (8u)
#define PIN_D9   (9u)
#define PIN_D10   (10u)
#define PIN_D11   (11u)
#define PIN_D12   (12u)
#define PIN_D13   (13u)
#define PIN_D14   (14u)
#define PIN_D15   (15u)

I see no pin 28 anywhere. Pin A5 would be referred to either as A5, PIN_A5, or 19.

analogRead() is slow, slower I'd guess than switching the pin mode as necessary.

a7

Correct, my bad. I was copying the code for an RP2040 and mistyped.

How would you force the switch back to digital mode?

Just before the digital read.

Is that something unique to Uno R4? I'm not very familiar with the R4's chip.

For most types of Arduino, like like those with AVR chips, there is no separate "digital input" or "analog input" mode. It's just an input, which is the default anyway. The pins which can be either analog or digital inputs are permanently connected to 2 internal circuits. One can detect the digital status of the pin. The other connects it via an internal multiplexer to the ADC circuit. But that multiplexer doesn't prevent the other circuit from detecting the digital status of the pin.

(On classic Nano 3, pins A6 & A7 are analog inputs only. They are connected only to the internal multiplexer which connects them to the ADC. But there are no circuits to detect the digital status of the pins, unlike the other A0-A5 pins which do have those.)

So unless the R4's chip really is deferent to what I described, I think there must be some other problem here.

Sorry - missed this was an R4 - I have no idea how it manages the ports.

I agree. I tried this this morning with Uno - no pinMode changes needed. Alternated digitalRead and analogRead,...