Receiving serial input in the format <01,1000>

I am a complete Arduino beginner and I am having troubles with what I assume is a very basic task. I have found tutorials and examples such as this one: https://forum.arduino.cc/index.php?topic=396450
but I believe that significantly overcomplicates things, because they are trying to receive both numbers and letters.

I just want to turn a serial input of <01,1000> (from a raspberry pi that my friend is programming) into two variables to use for controlling location (01) and duration (1000) of a valve opening.

I will most likely be seeing locations between 01 and 32, as well as durations between 500ms and 30000ms. Is there any reason to send durations as 00500 instead of 500 because of the possibility of values with more significant figures (ex: 29000)?

I don’t have any code to post because I really don’t know where to start, sorry about that.

Also a little more detail on the process if you’re interested, although I don’t think its necessary for answering my question:
My friend is programming a raspberry pi to control an Arduino that I have to program, it will be sending it serial data somehow, in a format that we agree on, he has proposed the above format of <01,1000>. The Arduino will take that data, and then use it to open one of 32 valves to dispense liquid for between 500 and 30000 milliseconds. The commands from the pi will be coming in pretty slowly, probably every 10-20 seconds or so.

Thanks for the help!

Robin's examples don't overcomplicate things; they provide a reliable way of communication.

There is no reason to send leading zeroes, it can make life more complicated as a leading zero indicates that the representation of the number is in octal (and e.g. 08 is invalid in that case).

clutchcurl:
I am a complete Arduino beginner and I am having troubles with what I assume is a very basic task. I have found tutorials and examples such as this one: https://forum.arduino.cc/index.php?topic=396450
but I believe that significantly overcomplicates things, because they are trying to receive both numbers and letters.

As an admitted "complete beginner", you don't even know what you don't know.

Go with the tutorial that you cited, it's exactly what you need.

Which one of the examples he posted should I try to use? I would like to learn how the code works so I can use it in my project effectively, but it would be nice to have it narrowed down a bit.

Thanks guys

The Pi is sending <01,234>.

Example 3 to start with, and then the parts about parsing and converting in the next examples.

clutchcurl:
but I believe that significantly overcomplicates things, because they are trying to receive both numbers and letters.

This piece of code from my Tutorial just needs a small change to parse two numbers

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
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    floatFromPC = atof(strtokIndx);     // convert this part to a float

}

As written it is expecting a piece of text, an integer and a floating point value. You just need to change it so it receives two integer values - like this

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
    integerFromPC = atoi(strtokIndx);     // convert it to an integer

    strtokIndx = strtok(NULL, ",");
    integer2FromPC = atoi(strtokIndx); // and this part

}

...R

Thanks everyone you've all been very helpful!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.