Loading...
Searching...
No Matches
PeripheralReceiver.ino

Makes the board an I2C peripheral at address 0x42. Prints the bytes a controller writes, and answers each read with a counter that goes up by one.

Uses PhyduinoWire::begin(uint8_t) with PhyduinoWire::onReceive() and PhyduinoWire::onRequest().

Wiring
SDA, SCL and GND to another board running a controller sketch.
Output
Serial Monitor at 115200 baud: the bytes the controller wrote, as text.
Note
The callbacks run inside the I2C interrupt, so keep them short.
// I2C peripheral at address 0x42: prints bytes a controller writes, and answers
// read requests with a counter. Pair with another board running a controller sketch.
#include <Wire.h>
volatile uint8_t counter = 0;
void onReceive(int count) {
(void)count;
while (Wire.available()) {
Serial.write(Wire.read());
}
}
void onRequest() {
Wire.write(counter++);
}
void setup() {
Serial.begin(115200);
Wire.onReceive(onReceive);
Wire.onRequest(onRequest);
Wire.begin(0x42);
}
void loop() {}
SerialUSBClass Serial
USB serial port, the one the Serial Monitor uses.
Arduino Wire (I2C) on the SDA/SCL header pins.
PhyduinoWire Wire
I2C on the header SDA / SCL pins.
void loop(void)
Written by the sketch: runs over and over after setup(), on core 0.
void setup(void)
Written by the sketch: runs once after reset, on core 0.