Garbage value on serial monitor using teensy 3.2

Abbasi

Member
Hello every one.
I am new user of teensy and i am using teensy3.2 i want to get float value from serial monitor but when i enter any float value it returns me accurate value but with garbage value like 0.00
its my code
Code:
float val;

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);

}

void loop() {
  // put your main code here, to run repeatedly:
while(Serial.available()==0){}
 val=Serial.parseFloat();

Serial.print("You Entered=");
Serial.println(val);
}
when i enter any number my output is like the picture that i have uploaded.
Please guide me how can i fix this issue.
 

Attachments

  • 1.PNG
    1.PNG
    47.9 KB · Views: 186
The problem is the SerMon doesn't send until the NEWLINE is created pressing Enter. Then the NewLine is in the data stream and it was triggering the next time through:
Code:
void setup() {
	// put your setup code here, to run once:
	Serial.begin(115200);
	while (!Serial && millis() < 4000 );
	Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
}

float val;

void loop() {
	Serial.print("Enter a number - float if you please = ");
	while (Serial.available() == 0) {}
	val = Serial.parseFloat();

	Serial.print("You Entered=");
	Serial.println(val);
	if ( 10 == Serial.peek() ) { Serial.read(); Serial.println("Ate the NewLine!"); }
}

The parseFloat() uses PEEK to check the next char for the NewLine and leaves it queued:
Code:
int Stream::timedPeek()
{
  int c;
  unsigned long startMillis = millis();
  do {
    c = peek();
    if (c >= 0) return c;
    yield();
  } while(millis() - startMillis < _timeout);
  return -1;     // -1 indicates timeout
}
 
Last edited:
Back
Top