t41_to_t41_comm/i2c_slave.ino

35 lines
513 B
Arduino
Raw Normal View History

2025-02-17 06:25:24 +01:00
#include <Wire.h>
#define SLAVE_ADDRESS 0x20
byte reTxData;
volatile bool flag = false;
void setup()
{
Wire.setClock(1000000);
Serial.begin(9600);
Wire.onReceive(receiveEvent);
Wire.onRequest(sendEvent);
Wire.begin(SLAVE_ADDRESS);
}
void loop()
{
if (flag == true)
{
Serial.print("Data received from Master: ");
Serial.println(reTxData);
flag = false;
}
}
void receiveEvent(int numBytes)
{
reTxData = Wire.read();
flag = true;
}
void sendEvent()
{
Wire.write(reTxData);
}