Go Down

Topic: IC2 communication  (Read 121 times) previous topic - next topic

Barahim

Hello

I am working on 4 arduino devices in which I want to communicate between 3 slaves writer and 1 master reader.

the problem is how can I make the master reader take the data from each slave and distinguish them into different variable??? 

this code for the master reader, while the three slaves sending a byte of these variable (x, y, z)

Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5 , 4, 3 ,2);
#include <Wire.h>
byte x = 0;
byte y = 0;
byte z = 0;
int street1= 0;
int street2= 0;
int street3= 0;

void setup() {
Serial.begin(9600);
lcd.begin(20,4);
lcd.setCursor(5,1);
lcd.print("Hello");
delay(2000);
lcd.clear();
Wire.begin();
}

void loop() {
   Wire.requestFrom(..............)
   .............
   .................
   ..............
   ............

}
 

 :smiley-confuse:

J-M-L

#1
Dec 15, 2016, 06:50 pm Last Edit: Dec 15, 2016, 06:51 pm by J-M-L
Is that a school project where you are being asked to fill in the blank?

Put some code with one reader and one writer together and then ask yourself how to deal with more than one
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums

Barahim

Sorry, I did these blanks because I've not known how to do it.

These what I have done with one reader and one writer and they work properly.


Code: [Select]

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5 , 4, 3 ,2);
#include <Wire.h>
byte x = 0;
byte y = 0;
byte z = 0;
int street1= 0;
int street2= 0;
int street3= 0;

void setup() {
Serial.begin(9600);
lcd.begin(20,4);
lcd.setCursor(5,1);
lcd.print("Hello");
delay(2000);
lcd.clear();
Wire.begin();
}

void loop() {
   Wire.requestFrom(8, 1); //request 1 bytes form slave #8

   while (Wire.available()) {
    x = Wire.read();
   }
   street1= x;

delay(500);
lcd.setCursor(0,0);
lcd.print("street1 = ");
lcd.print(street1);

}




PaulS

So, when you request data from a different slave, store the response in a different variable.
The art of getting good answers lies in asking good questions.

Barahim

You mean I should use as it is following? :

Code: [Select]

   Wire.requestFrom(8, 1); //request 1 byte form slave #8

   while (Wire.available()) {
    x = Wire.read();
   }
   Wire.requestFrom(9, 1); //request 1 byte form slave #9

    while (Wire.available()) {
      y = Wire.read();
    }

   Wire.requestFrom(10, 1); //request 1 byte form slave #10

    while (Wire.available()) {
      z = Wire.read();
   }
   street1= x;
   street2= y;
   street3= z;


PaulS

Quote
You mean I should use as it is following? :
I don't see the need to have x, y, and z. You can just store the byte read into street1, street2, and street3. Note that with that code, there is no reason for street1, street2, or street3 to be int, when you send and receive bytes.
The art of getting good answers lies in asking good questions.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy