class Multiplexer{
public:
virtual void begin() = 0;
virtual float channel(uint8_t CHANNEL, bool state);
};
class Multi : public Multiplexer{
public:
Multi(uint8_t S0, uint8_t S1, uint8_t S2, uint8_t S3, uint8_t SIG){
pins[0] = S0;
pins[1] = S1;
pins[2] = S2;
pins[3] = S3;
this -> SIG = SIG;
}
void begin(){
for(int i = 0; i < 4; i++){
pinMode(pins[i], OUTPUT);
}
pinMode(SIG, OUTPUT);
}
float channel(uint8_t CHANNEL, bool state){
for(int i = 0; i < 4; i++){
digitalWrite(pins[i], channel_pins[CHANNEL][i]);
}
digitalWrite(SIG, state);
return 0.0;
}
float channel(uint8_t CHANNEL){
for(int i = 0; i < 4; i++){
digitalWrite(pins[i], channel_pins[CHANNEL][i]);
}
return analogRead(SIG);
}
private:
uint8_t SIG;
uint8_t pins[4];
const bool channel_pins[16][4] = {
{0, 0, 0, 0}, //ch0
{1, 0, 0, 0}, //ch1
{0, 1, 0, 0}, //ch2
{1, 1, 0, 0}, //ch3
{0, 0, 1, 0}, //ch4
{1, 0, 1, 0}, //ch5
{0, 1, 1, 0}, //ch6
{1, 1, 1, 0}, //ch7
{0, 0, 0, 1}, //ch8
{1, 0, 0, 1}, //ch9
{0, 1, 0, 1}, //ch10
{1, 1, 0, 1}, //ch11
{0, 1, 1, 1}, //ch12
{1, 0, 1, 1}, //ch13
{0, 1, 1, 1}, //ch14
{1, 1, 1, 1} //ch15
};
};
Multi mult(13, 12, 11, 10, A0);
void setup() {
mult.begin();
Serial.begin(9600);
}
void loop() {
// mult.channel(15, 1);
// delay(500);
// mult.channel(15, 0);
// delay(500);
Serial.println(mult.channel(13));
delay(100);
}