How do i print this string and float value

// C++ code
//
float count ;
int count1 = 0;
int count2 = 3;
String data;

void setup()
{
Serial.begin(9600);

}

void loop()
{ while (Serial.available() > 0)

{
data = Serial.readString(); //first store content in data

while (Serial.available() > 0) {
  count = Serial.parseFloat();  // then store content in count

  Serial.println (data);
  Serial.print (count);         // print both
}

}
}

Cant print an output

Read the forum guidelines.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

The serial input basics tutorial has robust and non-blocking ways to receive serial data.

And now?

Please remember to use code tags when posting code

You are reading everything out of the input buffer and then only printing if there is something left in the buffer. Perhaps you intended to have a character to separate the string from the float? For example you might want "data = Serial.readStringUntil(',');" so it stops when it reaches a comma and THEN parses a float.

Note: Usually you would use '.print();' for the first parts of an output line and '.println();' for the last part.

// C++ code
//
float count ;
int count1 = 0;
int count2 = 3;
String data;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available() > 0)
  {
    data = Serial.readString(); //first store content in data

    while (Serial.available() > 0)
    {
      count = Serial.parseFloat(); // then store content in count

      Serial.println (data);
      Serial.print (count);         // print both
    }
  }
}

ive included the comma but nothing

I think that the problem is that the Artduino is WAY faster than the serial communications. When the ',' arrives, it is detected before any other character arrives in the buffer. Since the buffer is empty, the .parseFloat() and printing are not done.

This seems to work. Make sure you have the Serial Monitor set to "No line ending":

// C++ code
//
float value;
String label;

void setup()
{
  Serial.begin(115200);  // 9600 is so 1990's
}

void loop()
{
  if (Serial.available())
  {
    label = Serial.readStringUntil(','); //first store content in data
    value = Serial.parseFloat(); // then store content in count

    Serial.print(label);
    Serial.print(", ");
    Serial.println (value);         // print both
  }
}

as you stated its a bit too fast tho.
But actually tho, I didnt enter the ',' there is some delay and then, the float value 0.00 is display. Meaning this does not allow an unlimited time slot

Correct. The default timeout is 1 second. If you don't want to use a comma to separate your string and your float, what should your sketch use to detect the end of the string?

Yes as noted above the default timeout is 1 sec. You can reduce that with Serial.setTimeout()
The 0.00 the dodgy return parseFloat gives when it cannot parse the number

See Arduino Software Solutions for a range of example of how to read in Serial data, with their pro and cons.
See the Number Conversions section for a reliable way to convert chars to a float

but what happened to the option of leaving the program wait indefinitely for an input by the user?

Was there ever such an option? You can implement the wait in your sketch but the library has a timeout.

This will wait for the user to send the first digit:

    while (!isdigit(Serial.peek()))
        Serial.read(); // Throw away any non-digit

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