#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

// Define CD74HC4067 control pins
#define s0Pin 5 // Connect CD74HC4067 S0 to D1 (GPIO 17)
#define s1Pin 4 // Connect CD74HC4067 S1 to D2 (GPIO 16)
#define s2Pin 3 // Connect CD74HC4067 S2 to D3 (GPIO 18)
#define s3Pin 2 // Connect CD74HC4067 S3 to D4 (GPIO 19)
#define SIG_pin A3 // Connect CD74HC4067 SIG pin to A0 (GPIO 36)

// Holds incoming values from CD74HC4067
byte muxValues[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int chanel = 4;

// parameter oled
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// 'Loading-Tank_0001_Threshold-1', 53x27px
const unsigned char loading_tank [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 
	0x3f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 
	0x60, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x00, 
	0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 
	0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0e, 0x00, 0x00, 
	0x00, 0x00, 0x03, 0x80, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x18, 0x7c, 0x00, 0xf8, 0x01, 
	0xf0, 0xc0, 0x31, 0xff, 0x03, 0xfe, 0x07, 0xfc, 0x60, 0x33, 0xff, 0x87, 0xff, 0x0f, 0xfe, 0x60, 
	0x63, 0xff, 0x87, 0xff, 0x0f, 0xfe, 0x30, 0x67, 0xc7, 0xcf, 0x8f, 0x9f, 0x1f, 0x30, 0x67, 0x83, 
	0xcf, 0x07, 0x9e, 0x0f, 0x30, 0x67, 0x83, 0xcf, 0x07, 0x9e, 0x0f, 0x30, 0x67, 0x83, 0xcf, 0x07, 
	0x9e, 0x0f, 0x30, 0x67, 0xc7, 0xcf, 0x8f, 0x9f, 0x1f, 0x30, 0x03, 0xff, 0x87, 0xff, 0x0f, 0xfe, 
	0x00, 0x03, 0xff, 0x87, 0xff, 0x0f, 0xfe, 0x00, 0x01, 0xff, 0x03, 0xfe, 0x07, 0xfc, 0x00, 0x00, 
	0x7c, 0x00, 0xf8, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

	// Set CD74HC4067 control pins as outputs
  pinMode(s0Pin, OUTPUT);
  pinMode(s1Pin, OUTPUT);
  pinMode(s2Pin, OUTPUT);
  pinMode(s3Pin, OUTPUT);
	pinMode(SIG_pin, INPUT);

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
	// for(int x=0; x<128; x++) {
	// 	display.clearDisplay();
	// 	display.drawBitmap(x, 0, loading_tank, 53, 27, WHITE);

	// 	display.setTextSize(2); 
	// 	display.setTextColor(SSD1306_WHITE);                 
  // 	display.setCursor(10,50);
	// 	display.println("GALIH DEV");
	// 	display.display();
	// 	delay(1);
	// }

}

void loop() {
  // put your main code here, to run repeatedly:
	// Display captured data
  for (int i = 0; i < chanel; i++) {
    setMuxChannel(i); // Select a channel on the CD74HC4067
    muxValues[i] = analogRead(SIG_pin); // Read the value on that channel and store in array
		delay(500);
	}
	displayData();
	

  display.clearDisplay();
	display.setTextColor(SSD1306_WHITE); 
	display.setTextSize(3);               
	display.setCursor(0,0);
	display.println("SOON");
	display.display();
	
	delay(1000); 

}

void setMuxChannel(int channel) {
  // Set CD74HC4067 control pins to select the desired channel
  digitalWrite(s0Pin, bitRead(channel, 0));
  digitalWrite(s1Pin, bitRead(channel, 1));
  digitalWrite(s2Pin, bitRead(channel, 2));
  digitalWrite(s3Pin, bitRead(channel, 3));
}

void displayData() {
  Serial.println();
  Serial.println("Values from CD74HC4067:");
  Serial.println("======================");
  for (int i = 0; i < chanel; i++) {
    Serial.print("input I"); 
    Serial.print(i); 
    Serial.print(" = "); 
    Serial.println(muxValues[i]);
		delay(500);
  }
  Serial.println("======================");  
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
mux1:I15
mux1:I14
mux1:I13
mux1:I12
mux1:I11
mux1:I10
mux1:I9
mux1:I8
mux1:I7
mux1:I6
mux1:I5
mux1:I4
mux1:I3
mux1:I2
mux1:I1
mux1:I0
mux1:COM
mux1:S3
mux1:S2
mux1:S1
mux1:S0
mux1:EN
mux1:VCC
mux1:GND