I have a string that looks like this "10.00,20.00,-50.00," in which these are angle values and they have 2 decimals and can be negative. I want to separate them into 3 separate floats. Here is my current code in which I use sscanf. All I get is 0.00 and have no idea why. Any help is truly appreciated.

``````String recvString = "10.00,20.00,-50.00,";
float r,p,y;

void setup() {
Serial.begin(9600);
Serial.println(r);
int result = sscanf(recvString.c_str(), "%f,%f,%f,", &r, &p ,&y);
Serial.println(r);
}

void loop() {}
``````

The AVR implementation of `sscanf()` does not support parsing floating point numbers, thus you only get zeros out of it. You can't use the `%f` specifier with it. You need to convert these numbers by yourself with functions like `atof()` or similar.

Splitting a string into parts can be done many ways. You could use `sscanf()` to split up the string parts and then use something like `atof()` to actually convert to float. Or you use `strtok()` for splitting (see my answer to this question). Or you use one of the many other options, that you get, when googling for something like "Arduino split string" or "Arduino split c string".

Note: Using the `String` class in Arduino is easy for beginners, but not a good practice (dynamic allocation, heap fragmentation). Look at Majenkos blog for more information about that. Since you already used a function for C-strings, I would suggest moving completely to C-strings (which are basically only an array of `char`).

Use the indexOf function to find index then use the subString to get values like this:

``````String recvString = "10.00,20.00,50.00";
float r, p, y;

void setup() {
Serial.begin(9600);
//Serial.println(r);
//recvString.toCharArray(s, recvString.length() + 1);
//int result = sscanf(recvString.c_str(), "%f,%f,%f,", &r, &p ,&y);
int i1 = recvString.indexOf(',');
int i2 = recvString.indexOf(',', i1+1);
Serial.println(i1);
Serial.println(i2);

String firstValue = recvString.substring(0, i1);
String secondValue = recvString.substring(i1 + 1, i2);
String thirdValue = recvString.substring(i2 + 1);

Serial.println(firstValue);
Serial.println(secondValue);
Serial.println(thirdValue);

}

void loop() {
// put your main code here, to run repeatedly:

}
``````
• definitely not this way
– Juraj
Nov 24, 2020 at 5:41
• But it works so why not? Nov 24, 2020 at 16:28