#include <Multiplexer4067.h> // Multiplexer CD4067 library >> https://github.com/sumotoy/Multiplexer4067
#define s0 3
#define s1 4
#define s2 5
#define s3 6
#define x1 A0 // analog pin of the first mux
const uint8_t controlPins[] = { 3, 4, 5, 6 };
#define COM A0
// Multiplexer4067 mplex = Multiplexer4067(s0, s1, s2, s3, x1);
float readMux(int channel)
{
for(int i = 0; i < 4; i ++)
{
digitalWrite(controlPins[i], channel >> i & 1);
}
delay(10);
return analogRead(COM);
}
void setup() {
// initialize serial output for debugging
int baudRate = 9600;
Serial.begin(baudRate);
// setup pins for input or output
for (int i = 0; i < 4; ++i)
{
pinMode(controlPins[i], OUTPUT);
}
pinMode(x1, INPUT_PULLUP);
// pinMode(x1, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// for(int channel = 0; channel < 6; channel++) {
// Serial.print("Channel #");
// Serial.print(channel);
// Serial.print(": ");
// for(int i = 0; i < 4; i ++) {
// Serial.print(channel >> i & 1);
// Serial.println();
// }
// }
// int channelReading = mplex.readChannel(0);
int channelReading = readMux(0);
Serial.print("Reading: ");
Serial.println(channelReading);
delay(500); // 1 second
}