void loop()
    if ( Serial.available() > 0 )
    static char input[inputLength];

    static uint16_t i;

    char c = Serial.read();

    if ( c != '\n')
      input[i++] = c;

      input[i] = '\0';
      i = 0;

      uint16_t array[80];
      uint16_t j = 0;

      if ( !strncmp(input, "SEND", 4) )
        char* p = input + 4;

        while ( (p = strchr(p, ' ')) != NULL )
          array[j++] = strtol(p, &p, 16);


Hello I can't seem to figure out what this code is doing. I know the main goal but I can't figure out the proccess. If someone could please make the code dumber for me I would appreciate it :). Full code is here since its hard to copy code: https://anonfiles.com/z6ldWff0pf/no_lib_ino

  • Sorry, I don't want to download some random file from a random site. Please include all relevant code into your question. Where did you get that code? I stumble across the while loop. I currently don't see how this would work, as `p = strchr(p, ' ') will set the pointer p to the next occurrence of a space character. Then the code tries to convert that space character as a hexadecimal string to an integer. Are you sure, that is code actually works?
    – chrisl
    Oct 16, 2020 at 9:05
  • Yes im sure it works :)
    – Macaroni
    Oct 17, 2020 at 5:28

2 Answers 2


It's easier to follow if you indent it properly. I'll do that, and add comments as I go:

void loop() {
    if ( Serial.available() > 0 ) { // If there is something in the serial RX buffer

        static char input[inputLength]; // Create a working string to store data in
        static uint16_t i; // This is the current index in that working string

        char c = Serial.read(); // Read the next character from serial

        if ( c != '\n') // If it's not a "line feed" character...
            input[i++] = c; // then store it in the input string and increment i

        else { // Otherwise...
            input[i] = '\0'; // Terminate the string with a NULL to make it valid C
            i = 0; // Reset the index to 0 ready for next time

            uint16_t array[80]; // Create an 80-int array (temporary)
            uint16_t j = 0; // And an index variable

            if ( !strncmp(input, "SEND", 4) ) { // If the string starts with SEND
                char* p = input + 4; // create a "pointer" to the string that starts 4 characters in

                while ( (p = strchr(p, ' ')) != NULL ) // While there is a space somewhere in the string
                    array[j++] = strtol(p, &p, 16); // convert the first portion to a number, store it in the array, and advance the pointer to the next space.

                ir_start(array); // Send the array through IR
                Serial.println("WURKS"); // Tell us it worked

So you see it receives data through serial and stores it, byte by byte, in input until it receives \n at which point it looks to see if it starts with SEND, then effectively "cuts off" the SEND by only considering everything after the SEND. Then it slices the remainder of the string up by looking for the next space each time and convert each portion to an integer which it stores in an array.

It then sends that array.


I was dumb, turns out it was converting the hex into a int. I sent the array through serial and looked at it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.