// uart-slave-red.ino — Node 1 (slave A), Arduino UNO R3 // Week 11 — receive-only node on the shared UART bus at 9600 baud. // SoftwareSerial on D8 keeps the hardware UART (D0/D1) free for the USB monitor. // Reads every frame, verifies checksum, and acts only when DEST_NID matches. #include #define MY_NID 1 #define NID_MASTER 0 #define FRAME_START 0xFF #define CMD_LED 0x03 #define RX_PIN 8 // bus RX (master TX -> here) #define TX_PIN 9 // unused, required by SoftwareSerial constructor #define LED_PIN 7 // red LED via 220 ohm to GND SoftwareSerial busSerial(RX_PIN, TX_PIN); uint8_t buf[6]; uint8_t idx = 0; void handleFrame() { uint8_t dest = buf[1], src = buf[2], cmd = buf[3], payload = buf[4], chk = buf[5]; if ((uint8_t)(dest ^ src ^ cmd ^ payload) != chk) { Serial.println(F("RX bad checksum, frame dropped")); return; } if (dest != MY_NID) { Serial.print(F("RX frame for NID ")); Serial.print(dest); Serial.println(F(" - not me, ignored")); return; } if (cmd == CMD_LED) { digitalWrite(LED_PIN, payload ? HIGH : LOW); Serial.print(F("RX DEST_NID=1 (me) LED=")); Serial.println(payload ? "ON" : "OFF"); } } void setup() { Serial.begin(9600); // USB monitor busSerial.begin(9600); // UART bus on D8 pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); Serial.println(F("Slave A (NID 1, red LED) ready - Arduino UNO.")); } void loop() { while (busSerial.available()) { uint8_t b = busSerial.read(); if (idx == 0 && b != FRAME_START) continue; // resync: wait for START buf[idx++] = b; if (idx == 6) { idx = 0; handleFrame(); } } }