Hello guys,
Does anyone know why the last two lines appear with zero values? I typed the numbers 3.21, 3.56 and they appear, but then the unwanted "zeros" appear too!
float x = 0.00f; // for incoming serial data
float y = 0.00f;
//------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
//-------------------------------------------------------------------------------------------
void loop() {
if (Serial.available() > 0) { // send data only when you receive data:
x = Serial.parseFloat();
y = Serial.parseFloat();
Serial.print("First I received: "); // say what you got:
Serial.println(x);
Serial.print("Second I received: ");
Serial.println(y);
}
}
Your topic was MOVED to its current forum category as it is more suitable than the original as it is not an Introductory Tutorial
As to your problem, what do you have the Line Ending set to in the Serial monitor ? Whatever you have it set to will be appended to what you enter and you probably have it set to both NL and CR
I get alright; there is no follow up 0s.
First I received: 3.21
Second I received: 3.56
Check that you have chosen "No line ending" option in "Line ending tab"
of the Serial Monitor (Fig-1).
Figure-1:
Thank you Bob
Yes, NL and CR are defined
Thank you Mostafa
I set the Line Ending and it worked, but I need to make sure that the X and Y variables will contain the values I typed and not the zeros not shown now
Validity check:
long m = *(long*)&x; //x = 3.21 that has been entered from the Inputbox of SM
Serial.println(m, HEX); //shows: 404D70A5 for 3.21
union
{
long p;
float z;
} myData;
myData.p = m;
Serial.println(myData.z, 2); //shows: 3.21
Conclusion: without any doubt, the variable x holds 3.21 in binray32 format (404D70A5).
You need to validate the serial received, not just pass it blindly to the next process. How you do that depends entirely on what you consider valid.
float x = 0.00f; // for incoming serial data
float y = 0.00f;
//------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
//-------------------------------------------------------------------------------------------
void loop() {
if (Serial.available() > 0) { // send data only when you receive data:
x = Serial.parseFloat();
y = Serial.parseFloat();
if (x > 0 && y > 0) {
Serial.print("First I received: "); // say what you got:
Serial.println(x);
Serial.print("Second I received: ");
Serial.println(y);
}
}
}
Hmm! interesting. Thanks. I'll try. I do the same thing for the variable Y, right?
By the way, look how interesting!
Edison thanks for the reply.
Any non-zero value is valid in this case.
I get alright:
First I received: 0.10
Second I received: 0.20
Including negative values ?
well remembered Bob. Negative numbers must not be included.
in reality it doesn't matter what is printed, but that the variables X and Y have the values typed correctly
if (x != 0 && y != 0) {
What do you find interesting? Given the input, the following timeline occurs...
// upon hitting enter, pc takes 18.75 ms to send
void loop() {//t0, t1 = 13 ms to second loop(), when serial is checked and parsed...
if (Serial.available() > 0) { // t0 approximately 65 nanoseconds
x = Serial.parseFloat();// 4.1 ms
y = Serial.parseFloat()// ;4.1 ms
Serial.print("First I received: "); // rougly a few clock cycles, say 2 ms
Serial.println(x);// 65 nanoseconds
Serial.print("Second I received: ");// ~ 2ms
Serial.println(y);// 65 nanoseconds
}
}
Also, Edison is a title... or forum badge now...
Are you convinced with validity codes of post #6?
Sorry, but I haven't tested it yet. I'm new here and I'm still trying to get used to it.
1 Like
Sorry, I thought your name was Edison. As I said below, I'm new here and I don't know how this forum works.
I think that's why they took the title "god" away.
No, it's because there were too many of us.
(It was no fun being a god - not enough smiting for my liking)
Well, there's a lot of information for me to process here. My two neurons are going to have to work hard now. I'll test the suggestions and come back to let you know the result. Thank you guys.
1 Like