#include #define PIN 5 //the Wemos WS2812B RGB shield has 1 LED connected to pin 2 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800); const byte numChars = 32; char receivedChars[numChars]; char tempChars[numChars]; // temporary array for use when parsing // variables to hold the parsed data int redFromSender = 0; int greenFromSender = 0; int blueFromSender = 0; boolean newData = false; void setup() { pixels.begin(); // This initializes the NeoPixel library. Serial.begin(9600); Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value"); Serial.println("Enter data in this style "); Serial.println(); } void loop() { recvWithStartEndMarkers(); if (newData == true) { strcpy(tempChars, receivedChars); // this temporary copy is necessary to protect the original data // because strtok() used in parseData() replaces the commas with \0 parseData(); showParsedData(); newData = false; } setColor(0,redFromSender,greenFromSender,blueFromSender,100); } //simple function which takes values for the red, green and blue led and also //a delay void setColor(int led, int redValue, int greenValue, int blueValue, int delayValue) { pixels.setPixelColor(led, pixels.Color(redValue, greenValue, blueValue)); pixels.show(); delay(delayValue); } void recvWithStartEndMarkers() { static boolean recvInProgress = false; static byte ndx = 0; char startMarker = '<'; char endMarker = '>'; char rc; while (Serial.available() > 0 && newData == false) { rc = Serial.read(); if (recvInProgress == true) { if (rc != endMarker) { receivedChars[ndx] = rc; ndx++; if (ndx >= numChars) { ndx = numChars - 1; } } else { receivedChars[ndx] = '\0'; // terminate the string recvInProgress = false; ndx = 0; newData = true; } } else if (rc == startMarker) { recvInProgress = true; } } } //============ void parseData() { // split the data into its parts char * strtokIndx; // this is used by strtok() as an index strtokIndx = strtok(tempChars,","); // get the first part - the string redFromSender = atoi(strtokIndx); strtokIndx = strtok(NULL, ","); // this continues where the previous call left off greenFromSender = atoi(strtokIndx); // convert this part to an integer strtokIndx = strtok(NULL, ","); blueFromSender = atoi(strtokIndx); } //============ void showParsedData() { Serial.print("REd "); Serial.println(redFromSender); Serial.print("Green "); Serial.println(greenFromSender); Serial.print("Blue "); Serial.println(blueFromSender); }