// 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
}
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
}
}
}
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