// // hello.broadhop-LED.t1624.ino // broad-hop LED tiny1624 hello-world // // Neil Gershenfeld 11/14/23 // // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose, but must // acknowledge this project. Copyright is retained and // must be preserved. The work is provided as is; no // warranty is provided, and users accept all liability. // #define LED1 21 // Downstream Pins (Connect these to Node 1) #define RxOut D7 // Connect to Node 1's TX (Pin 21 on C3) #define TxOut D6 // Connect to Node 1's RX (Pin 20 on C3) // Upstream Port: Uses the USB-C cable to talk to Laptop #define PortIn Serial // Downstream Port: Talks to the chain of nodes HardwareSerial PortOut(1); // Global Variables (Keep these the same) char cmd; uint8_t arg, addr, address; void setup() { delay(2000); // Give USB time to wake up // 1. Setup LED pinMode(LED1, OUTPUT); digitalWrite(LED1, HIGH); // OFF // 2. Start Upstream (USB) PortIn.begin(921600); // Wait for Python script to connect (Optional, prevents missed packets) // while(!PortIn); // 3. Start Downstream (Pins D7/D6) PortOut.begin(921600, SERIAL_8N1, RxOut, TxOut); // 4. Set Sensor Pin pinMode(RxOut, INPUT); } void loop() { // // wait for a command // while (PortIn.available() == 0); cmd = PortIn.read(); // // all LED command? // if (cmd == 'L') { PortOut.write(cmd); while (PortIn.available() == 0); arg = PortIn.read(); if (arg & 0b1) digitalWrite(LED1,HIGH); else digitalWrite(LED1,LOW); } // // individual LED command? // 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(LED1,HIGH); else digitalWrite(LED1,LOW); } } // // address assignment command? // else if (cmd == 'a') { PortOut.end(); pinMode(RxOut, INPUT); delay(2); if (digitalRead(RxOut) == LOW) { // // nothing connected, last node // address = 0; PortIn.write(0); PortOut.begin(921600, SERIAL_8N1, RxOut, TxOut); } else { // // something connected, wait for hop count // PortOut.begin(921600, SERIAL_8N1, RxOut, TxOut); PortOut.write('a'); while (PortOut.available() == 0); arg = PortOut.read(); address = arg+1; PortIn.write(address); } } }