I’m trying to wrap my head around using binary addresses in a mux. Failing miserably. For the circuit I have six 16 channel analog multiplexers CD74HC4067 as slaves to an eight channel multiplexor CD54HC4051 master. They are bringing in 96 sensor readings (IR receiver diodes through Op Amps) into an array that, in turn, is used to constantly update lotsa LEDs through a series of shift registers (TLC5940) using the Arduino TLC5940 Library.
I’ve included the possible wiring scheme that the code is trying to control.
It probably needs work too.
Question: I’ve seen, and SEMI-understand matrixes that address 32 inputs (4 x 8 bits) but not quite understanding how I can address the six mux chips with 96 inputs. Or if I can even do it this way. Example from this forum: How to code for cascading multiplexers? Is it possible with this matrix of 96 inputs, and how? Am I barking up the wrong tree?
Code so far:
#include <Tlc5940.h> //Arduino TLC5940 Library controls the individual LEDs - "Pixels"
int ledCount = 96; //change LED "PIXEL" count in matrix...also number of IR sensor inputs
int thresh = 10; //value for usable distance reading from IR
// main structures to hold LED Pixel array on/off state, IR reciever readings
byte dsply_state[ledCount]; //pixel on-off grid, need for tlc5940
uint_16 recv_state[ledCount]; // IR Readings - FROM mux set-up
// Master IR -mux sensor pins for Arduino Pro MINI
#define M_S0 2
#define M_S1 4
#define M_S2 5
// Slave IR mux sensor pins
#define S_S0 6
#define S_S1 7
#define S_S2 8
#define S_S3 12
// ANALOG PIN :
#define InputFromMux A0
void setup() {
//Serial.begin(9600);
//tlc5940 Arduino library
Tlc.init(0);
// CONFIGURE ADDRESS PINS
pinMode(M_S0, OUTPUT);
pinMode(M_S1, OUTPUT);
pinMode(M_S2, OUTPUT);
pinMode(S_S0, OUTPUT);
pinMode(S_S1, OUTPUT);
pinMode(S_S2, OUTPUT);
pinMode(S_S3, OUTPUT);
pinMode(InputFromMux, INPUT);
}
void loop() {
// LOOP THROUGH ALL THE ADDRESSES OF THE MASTER and SLAVES.
for (int i = 0; i < ledCount; i++) {
digitalWrite(M_S2, i & 0b1000000);
digitalWrite(M_S1, i & 0b0100000);
digitalWrite(M_S0, i & 0b0010000);
digitalWrite(S_S3, i & 0b0001000);
digitalWrite(S_S2, i & 0b0000100);
digitalWrite(S_S1, i & 0b0000010);
digitalWrite(S_S0, i & 0b0000001);
delay(2);
//edited as per @Gerben. thanks so much
//get an individual sensor reading from IR receiver/opAmp mux, store it in a variable
int IRstate = analogRead(InputFromMux);
//populate receiving array with a reading
recv_state[i] = IRstate;
//populate display array for tlc5940 to push out
dsply_state[i] = recv_state[i];
} //end for(i...) loop
//run through display state array to check if the stored reading is greater than a threshold number
//if yes, then turn on the "pixel". If not, set it to zero
//
for (int x = 0; x < ledCount; x++)
if (dsply_state[x] >= thresh ) {
Tlc.set(dsply_state[x], 4000);
//or if reading from mux is zero
} else if (dsply_state[x] < thresh) {
Tlc.set(dsply_state[x], 0);
}
//tlc5940 Arduino library - update() clocks the chips
// move update(); outside the for(x...) loop for speed?
Tlc.update();
delay(10);
} //end For(x..) Loop
}//endMainLoop
digitalWrite(M_S2, i & 0b1000000)
not 0b110000. You do an anologRead, but never do anything with the read value (IRstate).0
on the left of all mask.IRstate = recv_state[x];
. Maybe you meantrecv_state[x] = IRstate;
instead. The value of right part is what you assign to the left part. I think what you need is to remove thefor( x ...)
loop and addrecv_state[i] = IRstate;
anddsply_state[x] = recv_state[x];