This code runs without issues, scoped and seen from serial output:
#include <Wire.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Wire.begin();
Wire.onReceive(receiveEvent);
while (!Serial);
}
void loop()
{
Serial.println("1");
Wire.beginTransmission(8);
Serial.println("2");
Wire.write(0x1);
Serial.println("3");
delay(100);
Wire.endTransmission();
delay(100);
Serial.println("4");
delay(500);
}
void receiveEvent(int howmany)
{
delay(100);
}
This one hangs on Wire.endtransmission():
#include <Wire.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Wire.begin(10);
Wire.onReceive(receiveEvent);
while (!Serial);
}
void loop()
{
Serial.println("1");
Wire.beginTransmission(8);
Serial.println("2");
Wire.write(0x1);
Serial.println("3");
delay(100);
Wire.endTransmission();
delay(100);
Serial.println("4");
delay(500);
}
void receiveEvent(int howmany)
{
delay(100);
}
The problem starts as soon as I assign an address in Wire.begin(). As I understand, a slave is also allowed to send data and temporarily becoming a master when addressing another device.