Read a muxed pin as analog and digital alternately

I'm thinking of using a multiplexer on which I'd like to connect both potentiometers and switches (it's for a rather big project and inserting a second mux isn't really a choice here).
I came up with the following Arduino pseudo-code to read the first five mux pins as analog and the last three as digital:

potVals[5];
switchVals[3];

for (int i = 0; i < 5; i++) {
  digitalWrite(CONTROL1, (i&7)>>2); 
  digitalWrite(CONTROL2, (i&3)>>1); 
  digitalWrite(CONTROL3, (i&1));
  potVals[i] = analogRead(muxPin);
}

pinMode(muxPin, INPUT);
for (int i = 0; i < 3; i++) {
  // give an offset of 5 to the control pins of the mux
  digitalWrite(CONTROL1, ((i+5)&7)>>2); 
  digitalWrite(CONTROL2, ((i+5)&3)>>1); 
  digitalWrite(CONTROL3, ((i+5)&1));
  switchVals[i] = analogRead(muxPin);
}

If in order to read an analog pin as digital the pinMode() function must be called, is there something that needs to be done in order to read that same pin again as analog?

If in order to read an analog pin as digital the pinMode() function must be called, is there something that needs to be done in order to read that same pin again as analog?

No need to use pinMode(). It's still an input, analog or digital. For a switch, use an external pull-up or -down resistor, use analogRead() and compare the result against 512. Higher and the switch is in one position, lower and the switch is in the other position. In other words, pretend the switch is another pot, but one that can only read zero (or close to it) or 1023 (or close to it).

You can even connect several switches to the same mux input. With careful selection of resistors, you can determine which switches are open/closed from a single analogRead().

PaulRB:
You can even connect several switches to the same mux input. With careful selection of resistors, you can determine which switches are open/closed from a single analogRead().

Just as long as they are not "Tact"-style pushbuttons with dodgy lubricant.

The trip meter on my car is now acting up - the button will not switch readings or reset it when the car is cold and particularly not if it has been raining, and I suspect it randomly resets sometimes.

And I have a number of computer monitors using the resistor array to multiplex the control buttons which respond to the buttons or spontaneously operate in random manners. Likewise older MP3 players.

It seems to me that this concept has a limited lifespan if there is anything doubtful about the quality of the switches/ pushbuttons. In the case of some of the computer monitors, replacement of buttons or disconnection of the button board itself has not stopped the ciphers, and I suspect the control chip itself has developed leakage.

Would there by any problem with using analogRead() for the potentiometers, and digitalRead() for the switches, on the same analog input pin?

As long as you properly define the pin using the A0 through A5 designation (or however many analog ports there are), instead of just 0 through 5, seems it would work properly.

Would there by any problem with using analogRead() for the potentiometers, and digitalRead() for the switches, on the same analog input pin?

Yes, it will not work.

As long as you properly define the pin using the A0 through A5 designation (or however many analog ports there are), instead of just 0 through 5, seems it would work properly.

Why do you think that?
How would you wire it up?
The difference between a digital input and analogue input is simply the analogue input gets routed to the A/D. If you read an analogue voltage from a digital pin then it simply applies the logic thresholds in the data sheet and these are not very accurately measured there. They just state the minimum and maximum for a state.

There is no way that the switch and pot can be independent when you wire them together.

Grumpy_Mike:
There is no way that the switch and pot can be independent when you wire them together.

The original post stated the pots and switches were being fed to a single analog input through a multiplexer.
I was making the assumption that the switches each had their own pullup or pulldown resistors prior to the multiplexer to provide proper levels to the multiplexer input.

PaulRB:
No need to use pinMode(). It's still an input, analog or digital. For a switch, use an external pull-up or -down resistor, use analogRead() and compare the result against 512. Higher and the switch is in one position, lower and the switch is in the other position. In other words, pretend the switch is another pot, but one that can only read zero (or close to it) or 1023 (or close to it).

You can even connect several switches to the same mux input. With careful selection of resistors, you can determine which switches are open/closed from a single analogRead().

I have been doing this till now, but I thought that since it's not necessary to read a switch as analog, why not read it as digital which is faster. Since they are multiplexed and they don't get mixed in their circuitry, is it possible to just call pinMode() in setup() and then use both analogRead() and digitalRead() on the same pin, depending on which multiplexer pin I'm currently reading?

Yes.

And digitalRead() will be faster, but compared to the time it takes to set up the multiplexer, the difference between that and analogRead() won't make much difference, I suspect.

Just be careful, if you do analogRead(0) it will read from pin A0, but digitalRead(0) reads from digital pin 0. You need to specifically use A0 as the pin designation.

david_2018:
Just be careful, if you do analogRead(0) it will read from pin A0, but digitalRead(0) reads from digital pin 0. You need to specifically use A0 as the pin designation.

or digitalRead(14) will also read from analog pin 0, right?
Thank you all for your help!

or digitalRead(14) will also read from analog pin 0, right?

On a Uno and Arduinos with the same processor yes.