Arduino I2C Wire.onReceive hangs after a few loops I have an Arduino Leonardo (master) and a Polulu A-star 328 PB(slave). I intend to use the slave to actuate some servos. I've modified the code from https://www.arduino.cc/en/Tutorial/MasterWriter to also blink the builtin led of the slave and it hangs after a few loops. Without the blink, it works fine. With blink, it hangs after a few loops.

Master:


#include &lt;Wire.h&gt;

void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop() {
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting
  x++;
  delay(2000);
}


Slave:

#include &lt;Wire.h&gt;

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Wire.begin(8);                // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);           // start serial for output
}

void loop() {
delay(100);
}

void receiveEvent(int howMany) {
while (1 &lt; Wire.available()) {
  char c = Wire.read(); 
  Serial.print(c);
}
int x = Wire.read();
Serial.println(x);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}


Serial monitor:

x is 16
x is 17
x is 18
x 


Any idea what's causing it and what can I do to further debug it or fix it?
Thank you!
After a bit of tinkering, I noticed that removing the delay() call in the slave fixed it and there were no more hangs. Also, when using the Servo library to move a servo instead of blinking the led, Servo.write() hangs while Servo.writeMicroseconds() doesn't. I'd mark the answer that has an explanation for this strange behavior.