I've been using Structs to send data between two UNOs successfully for a while now. Recently I tried to switch from a MasterSender/SlaveReader configuration to a MasterReader/SlaveSender configuration.
In doing so, my true and tested method of sending a Struct byte-wise through the wire started giving weird results. In debugging my original code and trying to reduce it to the simplest form that would give me the error I arrived at the attached code.
If you look in the void requestEvent() function you will see that I commented out a Wire.write("12"); command, and substitute it by two byte-wise commands, sending '1' and '2' separately. When I execute the code with the Wire.write("12"); the results on the other end are as expected (i.e., the Master Reader obtains "12"). However, if i try to send each digit individually, then I get the same garbage every time (I get the digit "2" followed by the character "ÿ" regardless of what bytes I send). I should note that I also tried sending ("1") instead of ('1') but the results were the same.
Help please!!!
#include <Wire.h>
void setup()
{
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop()
{
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
Wire.write('1');
Wire.write('2');
//Wire.write("12");
}