Go Down

Topic: while(Wire.available () - 1 master many slave arduino  (Read 89 times) previous topic - next topic

caslor

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 ?  :

Code: [Select]

  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");
      }

PaulS

Quote
can i use something like that and avoid  while loop ?  :
Avoid what while loop?

Why are you asking for two bytes and then only reading one? What do you think will happen to the other byte?
The art of getting good answers lies in asking good questions.

caslor

Avoid what while loop?
in most of the exambles they are using this : while (Wire.available()) {


so i was wondering if i could avoid the   "while" 

Why are you asking for two bytes and then only reading one? What do you think will happen to the other byte?
my mistake... that was from an other code i had..  so lets say that i am asking one only byte and not two

PaulS

If you ask your neighbor for an egg, do you need to use ANY kind of loop to receive the input from your neighbor?

You need some kind of loop to read the data if there is more than one byte to read. One byte == NO LOOP!
The art of getting good answers lies in asking good questions.

GolamMostafa

#4
Sep 20, 2018, 03:19 pm Last Edit: Sep 20, 2018, 03:19 pm by GolamMostafa
In order to read 2-byte data from your I2C Slave with address 0x03, you have executed the following instruction:
Code: [Select]
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:
Code: [Select]
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();
}

 

Go Up