30 lines
548 B
C++
30 lines
548 B
C++
#include <Arduino.h>
|
|
#include <Wire.h>
|
|
|
|
volatile uint8_t receivedValue = 0;
|
|
|
|
void setup() {
|
|
Wire.begin(0x42);
|
|
Wire.onReceive(receiveEvent);
|
|
Wire.onRequest(requestEvent);
|
|
Serial.begin(9600);
|
|
}
|
|
|
|
void loop() {
|
|
}
|
|
|
|
void receiveEvent(int howMany) {
|
|
if (howMany > 0) {
|
|
receivedValue = Wire.read();
|
|
Serial.print("Received from master: ");
|
|
Serial.println(receivedValue);
|
|
}
|
|
}
|
|
|
|
void requestEvent() {
|
|
uint8_t reply = (receivedValue > 100) ? 2 : 4;
|
|
Wire.write(reply);
|
|
Serial.print("Sent to master: ");
|
|
Serial.println(reply);
|
|
}
|