Data in Serial Monitor Displays Incorrectly

Hi all,

I'm trying to do something basic which I thought would work. I currently just want to enter data into the Serial monitor and have it printed after the user does so. The code works to wait for data, but the Serial.print to display it afterwards appears to display garbage.

For example, when I enter "5", it outputs "53.00" or something like that.

What could be going on?

/*Calculate the hypotenuse of a triangle.
* 
* User shall input 2 sides of the triangle.
*  
* References:
*	https://www.tutorialspoint.com/c_standard_library/math_h.htm
* 
*/
#include<Arduino.h>
#include<math.h>

//using namespace std;

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

void loop()
{
	//variables
	double side1;
	int side2;
	double hypotenuse;   // hypotenuse

	Serial.println("Let's calculate the hypotenuse of a triangle.");
  Serial.print("Enter side 1...");

  while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }
     
  side1 = Serial.read();
  Serial.print("\nSide 1 = ");
  Serial.println(side1);   
  Serial.flush();   
  delay(3000);
  exit(0);
}

side1 is a double

side1 = Serial.read();

read() reads a byte - 53 is the ASCII character code for character 5
try

side1=Serial.parseFloat()

That worked well, thank! I tried a bit more in my code, but it's skipping on waiting for side2 around here:

 Serial.print("\nEnter side 2...");
  while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }

It just goes to end of program, prints side1, but misses side 2.
Any ideas why it's not waiting like it did the first time?

/*Calculate the hypotenuse of a triangle.
* 
* User shall input 2 sides of the triangle.
*  
* References:
*	https://www.tutorialspoint.com/c_standard_library/math_h.htm
        https://www.programmingelectronics.com/parseint/
* 
*/
#include<Arduino.h>
#include<math.h>

//using namespace std;

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

void loop()
{
	//variables
	double side1;
	int side2;
	double hypotenuse;   // hypotenuse

	Serial.println("Let's calculate the hypotenuse of a triangle.");
  Serial.print("Enter side 1...");

  while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }
     
  side1 = Serial.parseFloat();    //takes incoming characters and converts it to a number. This is read into the buffer altogether.  Default is data type long.
  Serial.flush(); 
    
  Serial.print("\nEnter side 2...");
  while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }
  side2 = Serial.parseFloat();

  // print data
  Serial.print("\nSide 1 = ");
  Serial.println(side1);   
  
  Serial.print("\nSide 2 = ");
  Serial.println(side2);   
  
  
  
  Serial.flush();   
  delay(3000);
  exit(0);
}


Serial.flush() flushes the output stream not the input so the newline is left in the input buffer
try

 side1 = Serial.parseFloat();   
  while(Serial.available()) Serial.read();   // flush input    
  Serial.print("\nEnter side 2...");

have a read of Serial input basics

I tried the code as you specified, and made a small edit to it based on the link you provided. It still didn't work (neither way did).

However, I noticed that after I typed in the 1st side, e.g. "5", if I hit another number almost immediately, the program worked.

But it does not "hang" for the 2nd side to be entered, as it's supposed to.

/*Calculate the hypotenuse of a triangle.
* 
* User shall input 2 sides of the triangle.
*  
* References:
*	https://www.tutorialspoint.com/c_standard_library/math_h.htm
  https://www.programmingelectronics.com/parseint/
  https://forum.arduino.cc/t/serial-input-basics-updated/382007
* 
*/
#include<Arduino.h>
#include<math.h>

//using namespace std;

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

void loop()
{
	//variables
	double side1;
	double side2;
	double hypotenuse;   // hypotenuse

	Serial.println("Let's calculate the hypotenuse of a triangle.");
  Serial.print("Enter side 1...");

  while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }
     
  side1 = Serial.parseFloat();    //takes incoming characters and converts it to a number. This is read into the buffer altogether.  Default is data type long.
  //Serial.flush(); 

  /*  
  Serial.print("\nEnter side 2...");
  while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }
  */


  while(Serial.available())
  { Serial.read();    // flush input buffer  
  }  

  Serial.print("\nEnter side 2...");
  side2 = Serial.parseFloat();
 

  // print data
  Serial.print("\nSide 1 = ");
  Serial.println(side1);   
  
  Serial.print("\nSide 2 = ");
  Serial.println(side2);   
  
  
  
  Serial.flush();   
  delay(3000);
  exit(0);
}




Your forgot to add this before the entry for side2 like you did wiyh side1

 while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }

I tried that too, and it didn't work either.

Side2 code:

//while(Serial.available())
  while (Serial.available() == 0) 
  { Serial.read();    // flush input buffer  
  }  

  Serial.print("\nEnter side 2...");
  side2 = Serial.parseFloat();

Full code:

/*Calculate the hypotenuse of a triangle.
* 
* User shall input 2 sides of the triangle.
*  
* References:
*	https://www.tutorialspoint.com/c_standard_library/math_h.htm
  https://www.programmingelectronics.com/parseint/
  https://forum.arduino.cc/t/serial-input-basics-updated/382007
* 
*/
#include<Arduino.h>
#include<math.h>

//using namespace std;

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

void loop()
{
	//variables
	double side1;
	double side2;
	double hypotenuse;   // hypotenuse

	Serial.println("Let's calculate the hypotenuse of a triangle.");
  Serial.print("Enter side 1...");

  while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }
     
  side1 = Serial.parseFloat();    //takes incoming characters and converts it to a number. This is read into the buffer altogether.  Default is data type long.
  //Serial.flush(); 

  /*  
  Serial.print("\nEnter side 2...");
  while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }
  */


 //while(Serial.available())
  while (Serial.available() == 0) 
  { Serial.read();    // flush input buffer  
  }  

  Serial.print("\nEnter side 2...");
  side2 = Serial.parseFloat();
 

  // print data
  Serial.print("\nSide 1 = ");
  Serial.println(side1);   
  
  Serial.print("\nSide 2 = ");
  Serial.println(side2);   
  
  
  
  Serial.flush();   
  delay(3000);
  exit(0);
}


You need to read your code more carefully.

You have a line which waits for Serial input.

while (Serial.available() == 0) 
  {// waits until data is entered into serial monitor
  }

You have a line which reads the input from Serial and assigns it to a variable.

 side1 = Serial.parseFloat();

Finally you have code which empties the input buffer.

while(Serial.available())
  { Serial.read();    // flush input buffer  
  }  

If this was what you wanted for side1, then you should be able to duplicate it for side2.

I got it now, thanks. I needed to include the flush buffer before asking for input again between side1 and side2.

/*Calculate the hypotenuse of a triangle.
* 
* User shall input 2 sides of the triangle.
*  
* References:
*	https://www.tutorialspoint.com/c_standard_library/math_h.htm
  https://www.programmingelectronics.com/parseint/
  https://forum.arduino.cc/t/serial-input-basics-updated/382007
* 
*/
#include<Arduino.h>
#include<math.h>

//using namespace std;

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

void loop()
{
	//variables
	double side1;
	double side2;
	double hypotenuse;   // hypotenuse

	Serial.println("Let's calculate the hypotenuse of a triangle.");
  
// Side 1
  Serial.print("Enter side 1...");
  while (Serial.available() == 0)   // waits until data is entered into serial monitor
  {
  }
  side1 = Serial.parseFloat();    //takes incoming characters and converts it to a number. This is read into the buffer altogether.  Default is data type long.
 

// flush input buffer, do before asking for new input.  Notes: https://forum.arduino.cc/t/serial-input-basics-updated/382007
  while (Serial.available() > 0) 
  { Serial.read();    // flush input buffer  
  } 

// Side 2
  Serial.print("\nEnter side 2...");
  while (Serial.available() == 0)      // waits until data is entered into serial monitor
  {
  }
  side2 = Serial.parseFloat();
 

// print data
  Serial.print("\nSide 1 = ");
  Serial.println(side1);   
  
  Serial.print("Side 2 = ");
  Serial.println(side2);
  
    
  Serial.flush();   //empty serial buffer output
  exit(0);          // ends program
}


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