I'm guessing that the single line of code on the slave device sends the two bytes to the master (Defined by sizeof() ?). Question being, although this works great, what exactly is going on here? While(Wire.available()) // slave may send less than requested Master: Wire.requestFrom(2, 2) // request 2 bytes from slave device #2 Slave: Wire.write((byte *)&w, sizeof(unsigned int)) I played with it for a few hours before finding that this works: It looked like it was printing the first byte, then reading x into the second byte and overwriting the first, printing the value of x in between overwriting it. Results in two lines being printed to the serial monitor, even when I used Serial.print(x) and not Serial.println. (With w being an unsigned int) and then receiving it on the master with int x = Wire.read They also have to agree on the data structure being sent to do this. ![]() ![]() I've read that wire.write() sends one byte at a time, and that it is the master's job to receive the data and reconstruct it into the appropriate format. It works, but I don't understand how or why. I've got two set up and communicating to each other, but I've got a few questions. ![]() I've been reading about i2c and thought it sounded like a nice solution for communication between arduinos.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |