// Upload this to Node 1, Node 2, etc. // --- PIN DEFINITIONS --- #define LED_PIN 8 // Downstream Connection (To Next Node) #define RxOut 0 #define TxOut 1 // SLAVE INPUT: Listens to Previous Node via Pins 20/21 // On C3 Super Mini, "Serial0" is hardwired to 20/21 #define PortIn Serial0 // SLAVE OUTPUT: Talks to Next Node via Pins 0/1 HardwareSerial PortOut(1); // Global Variables char cmd; uint8_t arg, addr, address; void setup() { delay(1000); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, HIGH); // Start OFF // 1. Start Upstream Connection (Pins 20/21) PortIn.begin(921600); // pinMode(20, INPUT_PULLUP); // Safety: Prevent noise if wire is loose // 2. Start Downstream Connection (Pins 0 and 1) PortOut.begin(921600, SERIAL_8N1, RxOut, TxOut); // 3. Setup Sensor Pin pinMode(RxOut, INPUT); } void loop() { // Wait for command from Previous Node while (PortIn.available() == 0); cmd = PortIn.read(); // --- 'L' (All LEDs) --- if (cmd == 'L') { PortOut.write(cmd); // Pass to Next Node while (PortIn.available() == 0); arg = PortIn.read(); PortOut.write(arg); // Pass to Next Node if (arg & 0b1) digitalWrite(LED_PIN, LOW); else digitalWrite(LED_PIN, HIGH); } // --- 'l' (Specific LED) --- else if (cmd == 'l') { PortOut.write(cmd); while (PortIn.available() == 0); addr = PortIn.read(); PortOut.write(addr); while (PortIn.available() == 0); arg = PortIn.read(); PortOut.write(arg); if (addr == address) { if (arg & 0b1) digitalWrite(LED_PIN, LOW); else digitalWrite(LED_PIN, HIGH); } } // --- 'a' (Address Assignment) --- else if (cmd == 'a') { PortOut.end(); pinMode(RxOut, INPUT); delay(5); // Check: Do I have a neighbor? if (digitalRead(RxOut) == LOW) { // I am the Last Node address = 0; PortIn.write(0); // Tell Previous Node "I am 0" PortOut.begin(921600, SERIAL_8N1, RxOut, TxOut); } else { // I have a neighbor! PortOut.begin(921600, SERIAL_8N1, RxOut, TxOut); PortOut.write('a'); // Wake up Next Node while (PortOut.available() == 0); arg = PortOut.read(); // Wait for neighbor address = arg + 1; PortIn.write(address); // Tell Previous Node "I am neighbor+1" } } }