35 lines
513 B
Arduino
35 lines
513 B
Arduino
|
#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);
|
||
|
}
|