I have 16 cd74hc4067 multiplexors, conected on an arduino mega and each common pin of them goes through a 17th cd74hc4067 that connects to the mega as well. ofcourse i have used all available pins on the board to do so ( digital and analoge ) i only have A15 left unconnected. A0 will read all of them.
so i am using this code
/*
* Controlling and looping through a CD74HC4067's channel outputs
*
*
* Connect the four control pins to any unused digital or analog pins.
* This example uses digital pins
*0, 1, 2, and 3 for mux1.
*4, 5, 6 and 7 for mux2.
*8, 9, 10 and 11 for mux3.
*...............................................
*52, 53, A1 and A2 for mux14.
*A3, A4, A5 and A6 for mux15.
*A7, A8, A9 and A10 for mux16.
*A11, A12, A13 and A14 for mux17.
* Connect the common pin to any other available pin. This is the pin that will be
* shared between the 16 channels of the CD74HC4067. The 16 channels will inherit the
* capabilities of the common pin. For example, if it's connected to an analog pin,
* you will be able to use analogRead on each of the 16 channels.
*
*/
#include <CD74HC4067.h>
// s0 s1 s2 s3
CD74HC4067 mux1(0, 1, 2, 3); // create a new CD74HC4067 object with its four control pins
CD74HC4067 mux2(4, 5, 6, 7); // create a new CD74HC4067 object with its four control pins
CD74HC4067 mux3(8, 9, 10, 11); // create a new CD74HC4067 object with its four control pins
.......................................................................................................................................................................
CD74HC4067 mux14(52, 53, A1, A2); // create a new CD74HC4067 object with its four control pins
CD74HC4067 mux15(A3, A4, A5, A6); // create a new CD74HC4067 object with its four control pins
CD74HC4067 mux16(A7, A8, A9, A10); // create a new CD74HC4067 object with its four control pins
CD74HC4067 mux17(A11, A12, A13, A14); // create a new CD74HC4067 object with its four control pins
const int g_common_pin = A0; // select a pin to share with the 16 channels of the CD74HC4067
void setup()
{
pinMode(g_common_pin, INPUT); // set the initial mode of the common pin.
// This can be changed in loop() for for each channel.
}
void loop()
{
for (int x =0; x < 16; x++){
for (int i = 0; i < 16; i++) {
mux(x).channel(i);
mux17.channel(x);
analogRead(g_common_pin);
}
}
}
will this mux(x).channel(i); work ? while the mux1 ...... mux17
any suggestion to a cleaner code for my project ?