# Parse by using parseFloat()

**URL:** <https://forum.arduino.cc/t/parse-by-using-parsefloat/921312>\
**Category:** General Guidance\
**Created:** [November 3, 2021, 7:45am UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312 "2021-11-03T07:45:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![burstoua](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@burstoua](https://forum.arduino.cc/u/burstoua)\
**Post date:** [November 3, 2021, 7:45am UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/1 "2021-11-03T07:45:10Z")

</div>

I am trying to parse incoming data and extract two floating points by using parseFloat().  
The incoming data looks like this:

an1:1.40m  
an2:1.35m

And the code:

```cpp
void setup() {

  Serial1.begin(115200);
  Serial.begin(115200); 

}

void loop() {

  if (Serial.available()){

      float val1 = Serial1.parseFloat();
      float val2 = Serial1.parseFloat();
      Serial.println(val1);
      Serial.println(val2);
      Serial.println("---------------------");
  }
}

```

The result is:

```cpp
2.00
1.40
1.35
1.00
---------------------
2.00
1.40
1.00
1.35
---------------------
1.40
2.00
1.35
1.00
---------------------

```

I am wondering where 2.00 and 1.00 are coming from.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 3, 2021, 7:47am UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/2 "2021-11-03T07:47:36Z")

</div>

What do you have the Line ending set to in the Serial monitor ?

---

<div class="post-metadata">

**Author:** ![burstoua](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@burstoua](https://forum.arduino.cc/u/burstoua)\
**Post date:** [November 3, 2021, 7:56am UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/3 "2021-11-03T07:56:09Z")

</div>

"Newline", 115200 baud.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 3, 2021, 8:00am UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/4 "2021-11-03T08:00:56Z")

</div>

Try no line ending

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 3, 2021, 11:34am UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/5 "2021-11-03T11:34:37Z")

</div>

The 2.00 likely comes from the 2 in `an2:1.35m`

I would suggest to study [Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0) to understand how to deal with Serial input

---

<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:** [November 3, 2021, 1:47pm UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/6 "2021-11-03T13:47:46Z")

</div>

Here is demo code using the receive with end marker and parse with the [strtok()](http://www.cplusplus.com/reference/cstring/strtok/?kw=strtok) examples from the serial input basics tutorial. Also [the atof() function](http://www.cplusplus.com/reference/cstdlib/atof/?kw=atof) to convert strings to float data types.

```cpp
const byte numChars = 15;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;

float number_1;
float number_2;

void setup()
{
   Serial.begin(115200);
   Serial.println("<Arduino is ready> Enter two float numbers separated by commas");
   Serial.println("Like 1.40,1.35");
   Serial.println("make sure Newline is enabled in serial monitor line endings");
}

void loop()
{
   recvWithEndMarker();
   if (newData)
   {
      parseData();
      newData = false;
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial.available() > 0 && newData == false)
   {
      rc = Serial.read();
      if (rc == '\r') // ignore carriage return
      {
         return;
      }
      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void parseData()
{
   char *strings[3]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ","); // delimiters, semicolon
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }

   // convert string data to numbers
   number_1 = atof(strings[0]);
   number_2 = atof(strings[1]);

   Serial.print("number 1 = ");
   Serial.print(number_1);
   Serial.print(" number 2 = ");
   Serial.print(number_2);

   Serial.println(); // blank line
}

```

---

<div class="post-metadata">

**Author:** ![burstoua](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@burstoua](https://forum.arduino.cc/u/burstoua)\
**Post date:** [November 3, 2021, 1:55pm UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/7 "2021-11-03T13:55:11Z")

</div>

Yes, this is another approach that requires more coding.  
My initial intention was to use parseFloat(); but it seems to be difficult to realize.

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [November 3, 2021, 1:58pm UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/8 "2021-11-03T13:58:10Z")

</div>

> [@burstoua](#):
>
> My initial intention was to use parseFloat(); but it seems to be difficult to realize.

Yep. A path travelled by many. "I want to do it easy" but it doesn't quite work. Eventually, you bite the bullet and do it properly per the tutorial. It also offers much more flexibility if/when your requirements evolve/change.

---

<div class="post-metadata">

**Author:** ![burstoua](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@burstoua](https://forum.arduino.cc/u/burstoua)\
**Post date:** [November 3, 2021, 1:59pm UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/9 "2021-11-03T13:59:44Z")

</div>

Yes, it seems to be a good solution.

---

<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 2, 2022, 1:59pm UTC](https://forum.arduino.cc/t/parse-by-using-parsefloat/921312/10 "2022-05-02T13:59:59Z")

</div>

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