Fastest way to send serial commands and do something I am trying to send a lot of serial commands really fast. I basically need to change color of my LED strip many times per second (10 times per second should really be enough). If I do this over WiFi, it will eventually clutter up and make the ESP8266 unresponsive. I need to send the colors as hex, then convert them into usable values (HEX to RGB). Something tells me that's going to cost a lot of resources and the MCU is therefore spending a lot of time just converting the colors into RGB values. Here's a pseudo code example of what I am trying to achieve:

void loop() {
    if(Serial.available()) {
        if(Serial.read().first(8) == "0xABCDEF") {
            //0xABCDEF is basically my indicator saying "this is for individual LEDs"
            //Serial.read() can now be something like 0xABCDEF FF0000 00FF00 0000FF FFFFFF 000000
            char* colors[] = Serial.read().split(" ");
            colorLED(colors);

        }
    }
}

void colorLED(colors) {
    for(i = 1; i &lt; colors.length; i++) {
        strip.SetPixelColor(i, color[i]);
    }
}


I hope this example makes sense. I can send a serial command like 0xABCDEF FF0000 00FF00 0000FF FFFFFF 000000. 5 colors in HEX.

My real question is: How do I take a lot of inputs like above (or another way) and turn them into strings, so I can convert them to RGB? Is there a better way to send commands (like pure RGB values? but remember I need to send many)?. Here is how I convert my colors into RGB:

//hexColor can be "FF0000"
int number = (int)strtol(&amp;hexColor[0], NULL, 16);

int r = number &gt;&gt; 16;
int g = number &gt;&gt; 8 &amp; 0xFF;
int b = number &amp; 0xFF;
Simple answer: don't. 

Don't even consider strings in any form. Instead you want a state machine.

In pseudocode:


Start with an invalid count number (-1)
Read a character.
If that character is x then set your counter to 0.
Otherwise, if counter is 0 or more, convert the character to a number and place it into the right byte depending on the value of your counter
Increment counter
If counter is 6 then set your LED colours and set the counter to "invalid".
Return to start.


Or, in untested C:

static int8_t counter = -1;
static uint8_t r = 0;
static uint8_t g = 0;
static uint8_t b = 0;

if (Serial.available()) {
    char c = Serial.read();
    if (c == 'x') {
        counter = 0;
    } else {
        if (c &gt; 0x39) c -= 7;
        uint8_t v = c &amp; 0x0f;
        switch (counter) {
            case 0: r = v &lt;&lt; 4; break;
            case 1: r |= v;
            case 2: g = v &lt;&lt; 4; break;
            case 3: g |= v;
            case 4: b = v &lt;&lt; 4; break;
            case 5: b |= v;
        }
        counter++;
        if (counter == 6) {
            counter = -1;
            analogWrite(3, r);
            analogWrite(5, g);
            analogWrite(6, b);
        }
    }
}


The "clever" bit that may confuse you is:

if (c &gt; 0x39) c -= 7;
uint8_t v = c &amp; 0x0f;


That is, if the character is greater than a "9" (character 0x39) then it should be a-f. Subtracting 7 from it converts ASCII HEX from 0123456789ABCDEF to 0123456789:;&lt;=&gt;?.  If you then mask it with 0x0F you end up with a number between 0 and 15, or 0-F in hex.

The rest just takes each nibble and places it into the right half of the right colour variable.