#include #define SLAVE_ADDR 8 #define LED_PIN 13 void setup() { pinMode(LED_PIN, OUTPUT); Wire.begin(SLAVE_ADDR); // 初始化 I2C 从机 Wire.onReceive(receiveEvent); // 注册接收函数 Serial.begin(9600); Serial.println("Slave ready."); } void loop() { // 主循环无需操作,所有由中断处理 } void receiveEvent(int bytes) { while (Wire.available()) { char c = Wire.read(); Serial.print("Received: "); Serial.println(c); if (c == 'A') { digitalWrite(LED_PIN, HIGH); } else if (c == 'B') { digitalWrite(LED_PIN, LOW); } } }