An update. Sending multiple bytes at all during a requestEvent results in error.
I'm running on two Uno R3 arduinos.
Master uses requestFrom to ask for bytes. If I ask for one, it works, but if I ever ask for more than 1, they come back corrupted.
Master code:
#include <Wire.h>
int numReceived = 0;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200);
}
void loop()
{
numReceived = 0;
Wire.requestFrom(2, 2);
while(Wire.available())
{
byte c = Wire.read();
Serial.print("Received: ");
Serial.print(c);
Serial.println(" . ");
numReceived++;
}
delay(500);
Serial.print(" Received ");
Serial.print(numReceived);
Serial.println(" bytes. ");
}
Slave code:
#include <Wire.h>
byte val1 = 0;
byte val2 = 1;
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
void loop()
{
delay(100);
}
void requestEvent()
{
Wire.write(val1);
Wire.write(val2);
}
Output is :
Received: 1 .
Received: 255 .
Received 2 bytes.
What is happening here?
-Eric