I have an arduino device that captures incoming serial data from a sensor in the string that is made up of three numbers, separated by spaces, and ending with a letter, usually looks like this: -24 55.5 243t
I used to have an older sensor that outputted 3 integers, which was easy to parse using the following code. But now that the middle number is a float, my code breaks. I know that not many people recommend using the scanf() function anyway, and it appears it doesn't work for floats. I can't figure out how to use atof() on an array like this one. Is there a better option that I should try?
int x,z;
float y;
String testString = "-15 22.8 402t";
void setup()
{
Serial.begin(9600);
}
void loop() {
Serial.print("testString = ");
Serial.println(testString); //so you can see the captured string
char Carray[testString.length() + 1]; //determine size of the array
testString.toCharArray(Carray, sizeof(Carray));
Serial.print("Carray = ");
Serial.println(Carray);
sscanf (Carray, "%d %f %d", &x, &y, &z);
Serial.println("THE DATA:");
Serial.print("first= ");
Serial.println(x);
Serial.print("middle= ");
Serial.println(y);
Serial.print("last= ");
Serial.println(z);
delay(5000);
}