#define E1  5
#define E2 4
#define S0 18
#define S1 19
#define S2 22
#define S3 23
#define COM1 34
#define COM2 25
const uint8_t controlPins[] = { S0, S1, S2, S3 };

float readMux(uint8_t channel)
{
  // digitalWrite(E1, HIGH);  // Disable chip before changing channel.
  // delay(1);
  for(int8_t i = 3; i >= 0; i--)
  {
    int8_t b =  (channel >> i) & 1;
    digitalWrite(controlPins[i], b);
    // Serial.print(b);
    // delay(10);
  }
  // digitalWrite(E1, LOW);  // Enable chip after changing channel.
  delay(10);
  // Serial.print(": ");
  return analogRead(COM1);
}
void writeMux(uint8_t channel, uint8_t val)
{
  // digitalWrite(E1, HIGH);  // Disable chip before changing channel.
  // delay(1);
  for(int8_t i = 3; i >= 0; i--)
  {
    int8_t b =  (channel >> i) & 1;
    digitalWrite(controlPins[i], b);
    // Serial.print(b);
    // delay(10);
  }
  // digitalWrite(E1, LOW);  // Enable chip after changing channel.
  delay(10);
  // Serial.print(": ");
analogWrite(25,val);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(E1, OUTPUT);
  digitalWrite(E1,LOW);
  digitalWrite(E2,LOW);
    analogReadResolution(8);
    analogWriteResolution(8);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000); // this speeds up the simulation

   for(uint8_t i = 0; i < 2; i++)
  {
    float f = readMux(i);
    writeMux(i,(int)f);
    
    // pixels.setPixelColor(i, pixels.Color(0, f / 4095.0 * 255.0, 0));
    
    Serial.print(i);
    Serial.print(" = ");
    Serial.println(f);
  }
}
Loading
cd74hc4067
Loading
cd74hc4067