Input data to serial monitor

I am trying to input data to the serial monitor. I have been guided by an article in 'circuit basics' called 'how to read user input from arduino. The article gives a complicated example. I designed a simple example. The program waits for me to input a value for x and then reads it when entered and prints out the result. It compiles ok but there is a bug in the program. The program;
int x;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("input value for variable x: ");
while (Serial.available() == 0) {
}
x = Serial.parseInt();
Serial.print("x=");
Serial.println(x);
}
The result;
input value for variable x: x=7
input value for variable x: x=0
input value for variable x:
The second line x=0 should not be there. The third line waits for another input and then the same thing happens showing x=0 again. Can anybody tell me where the bug is in this program? I never entered a value of x=0.

/* Right Triangle - User Interactive via Serial Terminal
   M. Ray Burnette 20130125
   Arduino Nano 328P target processor
   Binary sketch size: 5,202 bytes (of a 30,720 byte maximum)
*/

#include "math.h"               // include the Math Library

#define BAUD 9600
#define timeoutPeriod 2147483647    // Long var... about 25 days

float a;
float b;
float h;
char junk = ' ';

void setup()                    // run once, when the sketch starts
{
  Serial.begin(BAUD);           // set up Serial library at 9600 bps
  Serial.setTimeout(timeoutPeriod); // default is 1 second

  Serial.println("Let's calculate a hypotenuse, h");
  Serial.println("               /|");
  Serial.println("             /  |");
  Serial.println("           /    |");
  Serial.println("      h  /      |");
  Serial.println("       /       b|");
  Serial.println("     /          |");
  Serial.println("   /            |");
  Serial.println(" /________a_____|");
  Serial.println("");
  Serial.println("");
  
}

void loop()
{
  Serial.println("Enter value for leg 'a', Press ENTER");
  while (Serial.available() == 0) ;  // Wait here until input buffer has a character
  {
      //Side 1
    a = Serial.parseFloat();        // new command in 1.0 forward
    Serial.print("a = "); Serial.println(a, DEC);

    while (Serial.available() > 0)  // .parseFloat() can leave non-numeric characters
    { junk = Serial.read() ; }      // clear the keyboard buffer
  }

  Serial.println("Enter value for leg 'b', Press ENTER");
  while (Serial.available() == 0) ;
  {
      //Side 2
    b = Serial.parseFloat();
    Serial.print("b = "); Serial.println(b, DEC);
    while (Serial.available() > 0)
    { junk = Serial.read() ; }

    h = sqrt ( a*a + b*b );

    Serial.print("hypotenuse = ");
    Serial.println(h, DEC); Serial.println();
  }
}

Much more complex:
Don't Cross The Streams (FP scientific calculator serial co-processor) - Community / Exhibition / Gallery - Arduino Forum

This is most likely happening because you have Carriage Return & New Line line ending selected in the monitor. parseInt looks for line endings ... it finds 2... thinks there must be 2 integers.

Select either CR or NL... and your problem should go away.

Too complicated for me. I prefer simple, understandable programming. Have a look at the Circuit Basics example which is much simpler. Although they could have made it a lot simpler by leaving out all those sensor readings. All I needed was a program that inputted a number.

The setial input basics tutorial may be of interest.

Sorry red car. Don't follow what you mean. Where do I add this CR or NL into my program or is it a keyboard thing? How have I had Carriage Return & New Line line ending selected in the monitor?

In the serial monitor... I showed a screen shot of it above. At the bottom of the monitor window ... the left drop down box "Both NL & CR" in the screen shot.

Thanks red car. I missed that in your first posting. I have to pick one or the other but not both. Have not looked at it yet but am now going to. It must be a default thing if both are selected.

Yep, either CR or NL, but not NL & CR.

If you don't then you need to handle the extra line ending when reading in your code.

Changing in the monitor is easier.

Hi red car, Had several problems. The CR , NL buttons had been hidden from view by the bottom taskbar. Hid the taskbar then could view them. Of all 4 selections only 'no line ending' fixed the bug. All good now. Thanks very much for the help.

Good to hear.. and yes, my mistake "No line ending" is recommended in this case... I was thinking of a slightly different issue.

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