You should not have done that
Can you assign one Arduino board as Master and everything else as Slave ? There will still be about 20 things that you have to do right.
When a Arduino is set as a Slave, it is also a Master. There is no need for Wire.end().
The Wire.end() releases the SDA and SCL pins. They will no longer be I2C pins. It is used in case those pins are going to be used for something else.
Don't use the Serial functions in a interrupt routine. The receiveEvent() and requestEvent() functions are called from a interrupt routine and the Serial functions use interrupts themselves.
If you expect a certain number of bytes, then I prefer to check for that. If you are receiving one byte then you can do this:
volatile byte data;
void receiveEvent( int howMany)
if( howMany == 1) // extra safety check, one byte should be received.
data = (byte) Wire.read();
You don't check the error returned by Wire.endTransmission(), so you don't know if the Wire library has detected a collision on the I2C bus.
The receiveEvent() is called from an interrupt routine from the Wire library. By calling Wire.end() from inside that interrupt routine, you disable the I2C hardware and stop the Wire library. That is not good. I can not tell what will happen, so for now let's say the result will be totally unpredictable.
@PerryBebbington You are more optimistic than me.
Until there is no good implemenation of a multi-master bus for Arduino boards, then there can be no good reason to use it.
@sterretje Could you read all these pages: How to use the Arduino Wire library