Here is demo code using the receive with end marker and parse with the strtok() examples from the serial input basics tutorial. Also the atof() function to convert strings to float data types.
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
}
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.