#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <CD74HC4067.h>

#define S0_PIN 26  // Assign GPIO pin numbers for S0, S1, S2, and S3
#define S1_PIN 25
#define S2_PIN 33
#define S3_PIN 32
#define ANALOG_PIN 36 // Assign the analog pin of ESP32 connected to the multiplexer

CD74HC4067 mux(S0_PIN, S1_PIN, S2_PIN, S3_PIN);

const int g_common_pin = 36; // select a pin to share with the 16 channels of the CD74HC4067

LiquidCrystal_I2C lcd(0x27, 20, 4); // Address LCD 20x4

void setup() {
  Serial.begin(9600);
  lcd.init(); // Inisialisasi LCD
  lcd.backlight(); // Nyalakan backlight LCD

  pinMode(g_common_pin, OUTPUT); // set the initial mode of the common pin.
	                               // This can be changed in loop() for for each channel.
}

void loop() 
{  
     int channel = 2;
    mux.channel(channel);
  
     // Read analog value from selected channel
     int sensorValue = analogRead(ANALOG_PIN);
  
  // Print the analog value
  Serial.print("Analog value: ");
  Serial.println(sensorValue);

    // Tampilkan nilai di LCD
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Channel: ");
    lcd.print(i);
    lcd.setCursor(0, 1);
    lcd.print("Value: ");
    lcd.print(sensorValue);
    delay(1000); // Tunda 1 detik
  }
}