it's only in testing mode but it works
SLAVE
#include <Wire.h>
//Mux control pins
int s0_1 = 8;
int s1_1 = 9;
int s2_1 = 10;
int s3_1 = 11;
int s0_2 = 8;
int s1_2 = 9;
int s2_2 = 10;
int s3_2 = 11;
//Mux in "SIG" pin
int SIG_pin_1 = 0;
int SIG_pin_2 = 1;
byte a[16];
void setup() {
Wire.begin(1); /* join i2c bus with address 8 */
//Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(9600); /* start serial comm. */
Serial.println("-= Slave =-");
pinMode(s0_1, OUTPUT);
pinMode(s1_1, OUTPUT);
pinMode(s2_1, OUTPUT);
pinMode(s3_1, OUTPUT);
pinMode(s0_2, OUTPUT);
pinMode(s1_2, OUTPUT);
pinMode(s2_2, OUTPUT);
pinMode(s3_2, OUTPUT);
digitalWrite(s0_1, LOW);
digitalWrite(s1_1, LOW);
digitalWrite(s2_1, LOW);
digitalWrite(s3_1, LOW);
digitalWrite(s0_2, LOW);
digitalWrite(s1_2, LOW);
digitalWrite(s2_2, LOW);
digitalWrite(s3_2, LOW);
}
void loop() {
//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
//for (int i_1 = 0; i_1 < 16; i_1 ++) {
//a[i_1] = readMux_1(i_1);
//delay(10);
//}
//for (int i_2 = 0; i_2 < 16; i_2 ++) {
//Serial.println(readMux_2(i_2));
//delay(10);
//}
}
int readMux_1(int channel_1) {
int controlPin_1[] = {s0_1, s1_1, s2_1, s3_1};
int muxChannel_1[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_1 = 0; i_1 < 2; i_1 ++) {
digitalWrite(controlPin_1[i_1], muxChannel_1[channel_1][i_1]);
}
//read the value at the SIG pin
int val_1 = analogRead(SIG_pin_1);
//return the value
return val_1;
}
int readMux_2(int channel_2) {
int controlPin_2[] = {s0_2, s1_2, s2_2, s3_2};
int muxChannel_2[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_2 = 0; i_2 < 2; i_2 ++) {
digitalWrite(controlPin_2[i_2], muxChannel_2[channel_2][i_2]);
}
//read the value at the SIG pin
int val_2 = analogRead(SIG_pin_2);
//return the value
return val_2;
}
void requestEvent() {
for (int i_1 = 0; i_1 < 16; i_1 ++) {
a[i_1] = readMux_1(i_1);
Serial.print("a[");
Serial.print(i_1);
Serial.print("] = ");
Serial.println(a[i_1]);
delay(10000);
}
Wire.write(a, 16); /*send string on request */
Serial.println("---------------------");
}
MASTER
#include <Wire.h>
void setup() {
Serial.begin(9600); /* begin serial comm. */
Wire.begin(); /* join i2c bus as master */
Serial.println("-= Master =-");
}
void loop() {
Wire.beginTransmission(1); /* begin with device address 8 */
//Wire.write("Hello Slave"); /* sends hello string */
//Wire.endTransmission(); /* stop transmitting */
Wire.requestFrom(1, 16); /* request & read data of size 9 from slave */
while (Wire.available()) {
byte c = Wire.read();/* read data received from slave */
Serial.println(c);
}
Serial.println("----------------------------");
delay(1000);
}