# Split value commmand+value from serial

**URL:** <https://forum.arduino.cc/t/split-value-commmand-value-from-serial/602576>\
**Category:** Programming\
**Created:** [July 20, 2019, 12:45pm UTC](https://forum.arduino.cc/t/split-value-commmand-value-from-serial/602576 "2019-07-20T12:45:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliodv](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juliodv/32/220784_2.png) [@juliodv](https://forum.arduino.cc/u/juliodv)\
**Post date:** [July 20, 2019, 12:45pm UTC](https://forum.arduino.cc/t/split-value-commmand-value-from-serial/602576/1 "2019-07-20T12:45:41Z")

</div>

Dear colleagues.

Hope you are doing fine!

I'm doing a program for getting comands from serial comunication sent from bluethooth.

We have following comands that arduino will reiceve from bluethooth.

(command,value)

cycles,200  
timer1,500  
timer2,500

I need split and save command in variable as command "name and value" for use as below.

void loop() {

var = 0;  
while (var \< cycles) {

digitalWrite(12, HIGH);  
delay(timer1);  
digitalWrite(12, LOW);  
delay(timer2);

var++;  
}

Someone has an example for it ?

Have a nice day!

Thanks

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [July 20, 2019, 1:51pm UTC](https://forum.arduino.cc/t/split-value-commmand-value-from-serial/602576/2 "2019-07-20T13:51:18Z")

</div>

Have a look at the parse example in [Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0).

If you can reduce the commands to a single character it will make the job of writing the Arduino program much easier. For example 'C' instead of "cycles" and "1" instead of "timer1"

Another way of simplifying things would be to send all three values as a single message, for example \<200,500,500\>. Then the Arduino would know which is which by the position in the message.

...R

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [July 20, 2019, 3:03pm UTC](https://forum.arduino.cc/t/split-value-commmand-value-from-serial/602576/3 "2019-07-20T15:03:54Z")

</div>

You would be far better off understanding and using the code from Robin2's tutorial. All of those String concatenations will eventually lead to memory fragmentation and random bugs/resets. [Here is a page that explains why not to use Strings](https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/)and some ways to use null terminated character arrays (strings) instead.

Read the[how to use this forum-please read stick](https://forum.arduino.cc/index.php/topic,148850.0.html)y to see how to post code and some advice on how to ask a good question.

What Arduino board are you using?

---

<div class="post-metadata">

**Author:** ![juliodv](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juliodv/32/220784_2.png) [@juliodv](https://forum.arduino.cc/u/juliodv)\
**Post date:** [July 20, 2019, 3:25pm UTC](https://forum.arduino.cc/t/split-value-commmand-value-from-serial/602576/4 "2019-07-20T15:25:35Z")

</div>

Hi Colleagues.

I try to implement a simple code for test the example.

The main idea is head HelloWorld from serial and execute a blink port number 13 with delay time as serial comand.

\<HelloWorld, 1000, 24.7\>

```cpp
if (messageFromPC == 'HelloWorld'){
var = 0;
while (var < 10) {
digitalWrite(led, HIGH);
delay(integerFromPC);  
digitalWrite(led, LOW);
delay(integerFromPC);
var++;
}
}

```

Below you can find my code

```cpp
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
int var = 0;

boolean newData = false;

#define led 13

//============

void setup() {
    Serial.begin(9600);
    Serial.println("Enter data in this style <HelloWorld, 12, 24.7>  ");
    Serial.println();
    pinMode(led, OUTPUT);
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
        parseData();
        showParsedData();
        newData = false;

if (messageFromPC == 'HelloWorld'){
var = 0;
while (var < 10) {
digitalWrite(led, HIGH);
delay(integerFromPC);  
digitalWrite(led, LOW);
delay(integerFromPC);
var++;
}
}
    }
    

    
}

//============

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
    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

}

//============

void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);
    Serial.print("Integer ");
    Serial.println(integerFromPC);
    Serial.print("Float ");
    Serial.println(floatFromPC);   
}

```

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [July 20, 2019, 4:24pm UTC](https://forum.arduino.cc/t/split-value-commmand-value-from-serial/602576/5 "2019-07-20T16:24:27Z")

</div>

> juliodv:  
> I try to implement a simple code for test the example.

Do you have a question?

...R

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [July 20, 2019, 4:55pm UTC](https://forum.arduino.cc/t/split-value-commmand-value-from-serial/602576/6 "2019-07-20T16:55:29Z")

</div>

```cpp
if (messageFromPC =='HelloWorld'){

```

There are a couple things wrong with that statement. You cannot compare strings like that. Use [strcmp()](http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp). The strcmp() function is one of the functions in [string.h](http://www.cplusplus.com/reference/cstring/), a library for searching, comparing and concatenating strings.

```cpp
'HelloWorld'

```

Single quotes for single characters ('H'), double quotes for strings (string literals):

```cpp
"HelloWorld"

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:56pm UTC](https://forum.arduino.cc/t/split-value-commmand-value-from-serial/602576/7 "2021-05-05T23:56:30Z")

</div>


