hello, i would like to send and receive with i2c protocol. my master adruino on my network receives information from the computer and he have to send it to others arduino and in same time, slaves send mesured values to the master arduino
how i can to do ?
The documentation is in the library reference section. Here is a tutorial:
hello, thank's for your reply, i have already read this page my network is based on. the probleme is when i do wire.Write("1"); on master i receive "111111". i think it's caused by Wire.requestFrom(8, 6); but when i remove it i can't send with wire.Write();
i will retry today, i may be done an error.
thank's a lot .
no....it don't work
djnaoki:
hello, thank's for your reply, i have already read this page
We must see your code. Nobody can guess your problem without it. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags which make the code looklike this
when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.
To send from Master to Slave/s. First make sure you wire them correctly. A4 and A5 for uno, 20 21 for mega.
in the setup function of master device, start up the library. Wire.being();
Do the same thing for the slave devices, but you have to declare an address for them so that you can use that address from the master device to send message to slaves. So in the slave device, Wire.begin(1);
1 could be any integer in this case.
Go back to master device.
//to send a string value
Wire.beginTransmission(1); //we want to send message to slave 1, so specify the address.
Wire.write(data.c_str());
Wire.endTransmission(1); //end the transmission since data is send
//to send an integer value
Wire.beginTransmission(1);
Wire.write(99);
Wire.endTransmission(1);
//To request data from device
Wire.requestFrom(1, byteCount); //1 is the address and byteCount is how many bytes target device will return
Hope it helps
hello, in first i thank you all for your reply.
i'm in master listener mode.
this is my code for the master :
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
String data = "message";
Wire.beginTransmission(8);
Wire.write(data.c_str());
Wire.endTransmission(8);
}
void loop() {
Wire.requestFrom(8, 6);
while (Wire.available()) {
char c = Wire.read();
Serial.println(c);
delay(500);
}
}
this is my code for the salve sender :
#include <Wire.h>
void setup() {
Wire.begin(8);
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop() {
delay(100);
}
void requestEvent() {
Wire.write("hello ");
}
void receiveEvent(int val)
{
Serial.println(Wire.read());
}
thank's
Don't do serial prints inside an ISR (which is what receiveEvent is).
thank's for reply.
so how i can see the received text ?
what do you think about :
void receiveEvent(int val)
{
String mydata = wire.read();
}
void loop()
{
Serial.println(Wire.read());
}
i have think to a other thing :
is there a way to know the numbers of bytes ask by the master via wire.requestFrom(, ); ?
like this, when i do
requestFrom(8, 6);
the slave know there are 6 bytes and go to the specific procedure
and whene i do
requestFrom(8, 19);
the slave go to a other procedure.
thank's again
is there a way to know the numbers of bytes ask by the master via wire.requestFrom(, ); ?
No. That's why you send to the slave first something, telling it what you want, then you do a requestFrom afterwards.
what do you think about :
void receiveEvent(int val)
{
String mydata = wire.read();
}
Not much. For one thing you aren't using the variable mydata in that example, and for another I wouldn't be using the String class.
sorry but i dont understand. for exemple in my case, the master is connected in midi to my computer. it send and receive data how the master can send midi value or fader value to the slave
thank's
Post your fixed code please.