I am in the process of designing a project, where two Arduino's will be talking to each other through I2C. One Arduino will act as a master and the other one will act as a slave.
I want the master to send specific commands to slave and I want the slave to respond to these commands. In short I want to have a I2C slave similar to I2C Digital Potentiometer.
I am trying to implement it using the built-in Wire library, but couldn't.
From the master, I am initiating the command by using the following code
Wire.beginTransmission(4); // transmit to device #4
Wire.write(0x01); // sends one byte
Wire.endTransmission();
and in the slave I have the following code
void receiveEvent(int numBytes)
{
byte received = Wire.read();
if (received == 0x01) {
Wire.write(received);
} else {
Wire.write(2);
}
}
void setup() {
Wire.begin(4);
Wire.onReceive(receiveEvent);
}
The slave receives the command from the master and is able to write the correct value. But I am not able to read the value back in master.
I tried to use a onReceive handler in master. But I found that onReceive can't be used in master. Apart from it I was not able to find a way to read this data from slave in master.
My so my question is how can we read the data that was sent by slave to master in response to a data that was sent by master?