Hello,
I have 2 layers of multiplexers that I am trying to use to reduce many analog inputs down to one. I have found some code for using a single multiplexer, which I am using for the first layer of multiplexers (I have paralleled the digital control pins to all 6 MUX's, so they all switch at the same time).
http://s211422029.websitehome.co.uk/electrostatics/mux.jpg
However, I am having trouble trying to control the MUX on 'LAYER B' to make it loop around the first 6 multiplexers before they all switch to the next channel.
Code I found for one MUX:
//Mux control pins
int a_s0 = 9;
int a_s1 = 10;
int a_s2 = 11;
int a_s3 = 12;
//Mux in "SIG" pin
int SIG_pin = 0;
void setup(){
pinMode(a_s0, OUTPUT);
pinMode(a_s1, OUTPUT);
pinMode(a_s2, OUTPUT);
pinMode(a_s3, OUTPUT);
digitalWrite(a_s0, LOW);
digitalWrite(a_s1, LOW);
digitalWrite(a_s2, LOW);
digitalWrite(a_s3, LOW);
Serial.begin(9600);
}
void loop(){
//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
for(int i = 0; i < 16; i ++){
Serial.println(readMux(i));
delay(1000);
}
}
int readMux(int channel){
int controlPin[] = {a_s0, a_s1, a_s2, a_s3};
int muxChannel[16][4]={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
{1,1,0,0}, //channel 3
{0,0,1,0}, //channel 4
{1,0,1,0}, //channel 5
{0,1,1,0}, //channel 6
{1,1,1,0}, //channel 7
{0,0,0,1}, //channel 8
{1,0,0,1}, //channel 9
{0,1,0,1}, //channel 10
{1,1,0,1}, //channel 11
{0,0,1,1}, //channel 12
{1,0,1,1}, //channel 13
{0,1,1,1}, //channel 14
{1,1,1,1} //channel 15
};
//loop through the 4 sig
for(int i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
//read the value at the SIG pin
int val = analogRead(SIG_pin);
//return the value
return val;
}
However, everything I've tried to control a second MUX has failed. Does anyone have any ideas of the best approach I should take?
//First layer (A) Mux control pins - these are paralleled across all 6 first layer Mux's
int a_s0 = 9;
int a_s1 = 10;
int a_s2 = 11;
int a_s3 = 12;
//Second layer (B) Mux control pins - switches between the 6 sub Mux's (b_s3 isn't really required, as it is always low, so could be grounded)
int b_s0 = 7;
int b_s1 = 6;
int b_s2 = 5;
int b_s3 = 4;
//96 Sensors to 'A0' anolog input pin through 2 layers of multiplexers
int SIG_pin = 0;
void setup(){
pinMode(a_s0, OUTPUT);
pinMode(a_s1, OUTPUT);
pinMode(a_s2, OUTPUT);
pinMode(a_s3, OUTPUT);
pinMode(b_s0, OUTPUT);
pinMode(b_s1, OUTPUT);
pinMode(b_s2, OUTPUT);
pinMode(b_s3, OUTPUT);
digitalWrite(a_s0, LOW);
digitalWrite(a_s1, LOW);
digitalWrite(a_s2, LOW);
digitalWrite(a_s3, LOW);
digitalWrite(b_s0, LOW);
digitalWrite(b_s1, LOW);
digitalWrite(b_s2, LOW);
digitalWrite(b_s3, LOW);
Serial.begin(9600);
}
//Loop the first (A) layer of multiplexer channels (C0-C15), allowing for the second (B) layer Mux to loop around the first layer between each channel switch.
void loop(){
//Loop the second (B) layer MUX
for(int i = 0; i < 6; i ++){
Serial.print(readMux(i));
delay(4);
}
}
int readMux(int channel){
int controlPin[] = {
b_s0, b_s1, b_s2, b_s3 };
int muxChannel[6][4]={
{
0,0,0,0 }
, //channel 0
{
1,0,0,0 }
, //channel 1
{
0,1,0,0 }
, //channel 2
{
1,1,0,0 }
, //channel 3
{
0,0,1,0 }
, //channel 4
{
1,0,1,0 }
//channel 5
};
//loop through the 4 sig
for(int i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
delay(4);
//read the value at the SIG pin
int val = analogRead(SIG_pin);
//return the value
return val;
}