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