/* James Khan Fab Academy networking and communication week week. Version 2 with node recognition. https://fabacademy.org/2021/labs/vancouver/students/james-khan/assignments/week13/ Used the code I developed for my input week as a starting point and modified for communication. https://fabacademy.org/2021/labs/vancouver/students/james-khan/assignments/week11/ This program is for an SEEED RP2040 board and communicates with my self made ATtiny board (with a different program). This program constantly listens for a message on two seperate serial ports Serial is the regular connection to the computer via USB SErial1 is the UART connection to my second board When a message comes in on either of the serial ports it "reads" the first character. If that character is NOT a "l" or "L" it sends the message on the other serial port. If that character is and "l" or "L" it means this board is being messaged directly and reads the next 3 characters and takes action depending on the characters. each character refers to one of 3 onboard leds RED, BLUE and GREEN in that order. a 1 turns that LED on, anything else turns that led off. */ String newstring ; // to hold incomming serial data. void setup() { pinMode(PIN_LED_R, OUTPUT); // RED LED digitalWrite(PIN_LED_R, HIGH); // initialize off pinMode(PIN_LED_G, OUTPUT); // GREEN LED digitalWrite(PIN_LED_G, HIGH); // initialize off pinMode(PIN_LED_B, OUTPUT); // BLUE LED digitalWrite(PIN_LED_B, HIGH); // initialize off delay(2000); Serial.begin(115200); // set's up the connection speed, needs to match the other program delay(200); Serial1.begin(115200, SERIAL_8N1); //defines a unique Serial addresses:default Serial is USB/computer, Serial1 is RX/TX or UART Serial.println("\n--- UART communication with XIAO RP2040 ---"); Serial.println("-------------------------------------------------------"); } void loop() { if (Serial.available()) { // If I send anything from the computer's serial monitor newstring=Serial.readString(); // save incomming string from serial if ((newstring[0]=='l') || (newstring[0]=='L')) { // is this message for me? if (newstring[1]=='1') digitalWrite(PIN_LED_R, LOW); // sets RED LED ON else digitalWrite(PIN_LED_R, HIGH); // sets RED LED OFF if (newstring[2]=='1') digitalWrite(PIN_LED_G, LOW); // sets GREEN LED ON else digitalWrite(PIN_LED_G, HIGH); // sets GREEN LED OFF if (newstring[3]=='1') digitalWrite(PIN_LED_B, LOW); // sets BLUE LED ON else digitalWrite(PIN_LED_B, HIGH); // sets BLUE LED OFF Serial.print("received [for me] and returned: "); // send back confirmation to sender Serial.println(newstring); } else Serial1.println(newstring); // if it's not for me send it over UART to other device // read it and send it out Serial1 (pins 0 & 1) digitalWrite(29, HIGH); delay(50); digitalWrite(29, LOW); // flashes LED to indicate received sent } if (Serial1.available()) { // If anything comes in Serial1 (my connected second board) newstring=Serial1.readString(); // save incomming string from serial if ((newstring[0]=='l') || (newstring[0]=='L')) { // is this message for me? if (newstring[1]=='1') digitalWrite(PIN_LED_R, LOW); // sets RED LED ON else digitalWrite(PIN_LED_R, HIGH); // sets RED LED OFF if (newstring[2]=='1') digitalWrite(PIN_LED_G, LOW); // sets GREEN LED ON else digitalWrite(PIN_LED_G, HIGH); // sets GREEN LED OFF if (newstring[3]=='1') digitalWrite(PIN_LED_B, LOW); // sets BLUE LED ON else digitalWrite(PIN_LED_B, HIGH); // sets BLUE LED OFF Serial1.print("received [for me] and returned: "); // send back confirmation to sender Serial1.println(newstring); } else Serial.println(newstring); // then send it to the computer's serial monitor digitalWrite(29, HIGH); delay(50); digitalWrite(29, LOW); // flashes LED to indicate received message } }