[Problem] Control MUX 8-1 by using if statement.

Greeting everybody. I have a problem with coding 74HC4051D analog - analog 8 -1 multiplexing to get data from 4 sensors. I only use S0 and S1 to control the mux (Select pin). Currently, I know my problem that I can not digitalRead 2 output S0 S1 at the same time or perhaps digitalWrite(S0 & S1) at the same time.

Because I have 4 sensors that mean I must control S0 and S1 as 00-01-10-11 to choose data from sensors that I want to process (Further, I will work with NodeMCU ESP8266 that has only 1 Analog pin). Using multiple conditions if statement is not working because the program is sequentially executed.

I can solve this problem by using 2 more pins as input then DigitalRead it, it works perfectly but it is not optimized and I looking forward to your better ideal ^^~

#define  S1 9
#define  S0 8
void setup() {
  Serial.begin(9600);
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
}

void loop() {
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  delay(100);
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  delay(100);
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  delay(100);
  digitalWrite(S0, HIGH);
  digitalWrite(S1, HIGH);
  delay(100);
    if( (digitalRead(S0)==LOW ) && (digitalRead(S1)==LOW) ){
        Serial.println("SENSOR1");
  }
    if( (digitalRead(S0)==HIGH ) && (digitalRead(S1)==LOW) ){
        Serial.println("SENSOR2");
  }
    if( (digitalRead(S0)==LOW ) && (digitalRead(S1)==HIGH) ){
        Serial.println("SENSOR3");
  }
    if( (digitalRead(S0)==HIGH ) && (digitalRead(S1)==HIGH) ){
        Serial.println("SENSOR4");
  }
}

I know my problem that I can not digitalRead 2 output S0 S1 at the same time or perhaps digitalWrite(S0 & S1) at the same time.

You can with direct port addressing:-
https://www.arduino.cc/en/Reference/PortManipulation
But there is absoloutly no need to do this because you only read the output when you have finished changing those bits. Why do you need to read these anyway? you write to them so you know what they are anyway.

#define  S1 9
#define  S0 8
void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
}

void loop() {
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
    if( (digitalRead(S0)==LOW ) && (digitalRead(S1)==LOW) ){
    Serial.println("SENSOR1");
  delay(100);

This is how I solve it ^^

Grumpy_Mike:
You can with direct port addressing:-
https://www.arduino.cc/en/Reference/PortManipulation
But there is absoloutly no need to do this because you only read the output when you have finished changing those bits. Why do you need to read these anyway? you write to them so you know what they are anyway.

Yes, you are right. Reading output is unnecessary. I know what they are but when connecting to ic mux it is no response data to A0 because my structure is not logical since I must put my main code below the state of output, not at the end. I mistake it with VHDL.

Can you post the code you are using, the one in reply#2 makes little sense because you never read anything.

One simple way of setting the selectors to each of the four possible combinations is to use a channel number that ranges from 0 to 3, and use the bits in that number to set the multiplex address pins.

 if(chaNum & 1) digitalWrite(S0, HIGH); else digitalWrite(S0, LOW);

if(chaNum & 2) digitalWrite(S1, HIGH); else digitalWrite(S1, LOW);

Then do your reading. You can put this in a for loop with chaNum as the loop index and put your readings in an array, addressed by the loop index.