onReceive() registers a callback function to be called when a peripheral device receives a transmission from a controller device.
This function is part of the Wire library. See the Wire main page for more information.
Wire.onReceive(handler)
handler: the callback function to be called when data is received. It should have the following signature:int numBytes (the number of bytes received from the controller device).void (nothing).Nothing.
void setup() {
Wire.begin(0x08); // Join I2C bus as peripheral with address 0x08
Wire.onReceive(receiveEvent); // Register callback function
}
void receiveEvent(int numBytes) {
while (Wire.available()) {
char c = Wire.read();
// Process received byte
}
}