-
Notifications
You must be signed in to change notification settings - Fork 0
/
slave2I2C.ino
50 lines (41 loc) · 1.11 KB
/
slave2I2C.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <Wire.h>
// Define Slave I2C Address
#define SLAVE_ADDR 10
// Define buffer size for received data
#define BUFFER_SIZE 50
// Buffer to store received data
char receivedData[BUFFER_SIZE];
void setup() {
// Initialize I2C communications as Slave
Wire.begin(SLAVE_ADDR);
// Function to run when data received from master
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
// Setup Serial Monitor
Serial.begin(9600);
Serial.println("I2C Slave Demonstration");
}
void receiveEvent() {
// Read the incoming data and store it in the buffer
int i = 0;
while (Wire.available()) {
receivedData[i] = Wire.read();
i++;
// Make sure the buffer doesn't overflow
if (i >= BUFFER_SIZE) {
break;
}
}
receivedData[i] = '\0'; // Null-terminate the string
// Print the received data to Serial Monitor
Serial.print("<Received>");
Serial.println(receivedData);
}
void requestEvent() {
Wire.write("Slave1:ily master"); // respond with message of 6 bytes
// as expected by master
}
void loop() {
// Additional processing can be done in the loop if needed
delay(500);
}