Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Garbage value on serial monitor using teensy 3.2

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    17

    Garbage value on serial monitor using teensy 3.2

    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.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	1.PNG 
Views:	20 
Size:	47.9 KB 
ID:	15910  

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,563
    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 by defragster; 02-15-2019 at 04:00 AM.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    17
    Now it's working . Thank you so much for your kind and quick reply..

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,563
    Glad to help - it is fun to solve new problems.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •