hi
i want to have 3 or 4 slave arduino and one master communicating via i2c
i want my master can read and write to the slaves but also can do and other functions
in most of the examples i have seen are for just one arduino (master) write or read from an other arduino (slave)
So i was wondering.. when i want to read from the slave arduinos can i use something like that and avoid while loop ? :
Wire.requestFrom(2, 2); // read from first slave
if (Wire.available()) {
char T = Wire.read();
if (T == 'M')
{
Serial.println("No locks");
}
else if (T == 'Y') {
Serial.println("ok");
}
Wire.requestFrom(3, 2); // read from second slave
if (Wire.available()) {
char T = Wire.read();
if (T == 'M')
{
Serial.println("No locks");
}
else if (T == 'Y') {
Serial.println("ok");
}
In order to read 2-byte data from your I2C Slave with address 0x03, you have executed the following instruction:
Wire.requestFrom(0x03, 2);
It is my understanding that the program control will be transferred to the next label of execution when the requested 2-byte data have arrived into the local unseen FIFO buffer of the Master. Therefore, there is really no need to check the status of data availability by executing the instruction Wire.available(); rather, we may go ahead reading the data from the local unseen buffer and saving them into known user buffer/variable. The codes could be:
byte x1 = Wire.read();
byte x2 = Wire.read();
or using loop as per Post#3
for(int i=0; i<2; i++)
{
dataByte[i] = Wire.read();
}