Problem using Serial.parseInt

Here is my code using Serial.parseInt():

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

void loop()
{
  int length=getAnswer();
  Serial.print(length);
}

void askLength()
{
  Serial.print("Overall Length of the Wall");
}

int getAnswer()
{
  int answer;
  answer=0;
  while(!Serial.available())
  {
    //nothing happens unless there is something typed into serial moniter    
  } 
  answer=Serial.parseInt();
  return answer;  
}

It will allow me to input a number and print it on the serial monitor , but after every number that appears on the serial monitor a zero shows after.

For example if I keyed in a 6, it would show : 6 0 on the serial moniter.

I need some help with this please

Serial.parseInt() returns 0 when it times out. You likely seeing the result of parsing the line terminator sent by the serial monitor.

I guess that's it! How can I get rid of it or not show it?

You could check the length returned by getanswer() and only print if it is not 0. Or you could turn off line terminators in the serial monitor.

That works,

I'm trying to send the getanswer() function to another function to be used in calculation..is there a way to get around the 0 timeout? or will that even affect the calculation?

zwood14:
That works,

I'm trying to send the getanswer() function to another function to be used in calculation..is there a way to get around the 0 timeout? or will that even affect the calculation?

Depends on how you use the return.

Are you sending both a '\r' and '\n' in the serial monitor?

no, this is what I have so far:

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

void loop()
{
  int finalLength=length();
  Serial.print(finalLength);  
}

void askLength()
{
  Serial.print("Overall Length of the Wall");
}

void askHeight()
{
  Serial.print("Overall height of the Wall");
}

int getAnswer()
{
  int answer;
  answer=0;
  while(!Serial.available())
  {
    //nothing happens unless there is something typed into serial moniter    
  } 
  answer=Serial.parseInt();
  return answer;
}

int length()
{
  int length=getAnswer();
  length=length+2;
  return length;  
}

Now when it times out, it shows a 2 instead of zero

Hello zwood14

In your final program, will you be using your askLength() and askHeight() functions to prompt the user to enter values, before you call getAnswer()?

If so, you could add the following code just before calling askLength() and askHeight(). It reads and discards any characters in the input buffer, including left over line terminators. It does mean that your user cannot type ahead, of course.

while (Serial.available())
{
        Serial.read();
}

You could even generalise your ask...() functions like this.

void promptForInput(char prompt[])
{
    while (Serial.available())
    {
        Serial.read();
    }
    Serial.print(prompt);
}

Then call it like this:

promptForInput("Overall Length of the Wall  ");

Regards

Ray

I kind of compressed it to the way I like doing it but here is my quick take on it.

int length = 0, answer = 0;

void setup()
{
Serial.begin(9600);
// Serial.setTimeout(500);
}

void loop()
{
getAnswer();
}

void getAnswer(){
while (Serial.available() != 0)
{
answer= Serial.parseInt();
length = answer +2;
Serial.print("answer = "); Serial.print(answer);
Serial.print(" length = "); Serial.println(length);
}
}

Ray,

Yes, I'm going to ask the user the askLength() and askHeigh() to tell the user to input numbers. So you're saying in the main loop add to prompt the user? or add that statement in the askLength() and askHeight() functions?

while (Serial.available())
{
        Serial.read();
}

Also, I'm having another problem. I want the length and height to be different numbers to do further calculations. But using the one getanswer() function it's not wanting to do that. Any suggestions of of how I could do that because it's been stumpin me since I've started

One of

or add that statement in the askLength() and askHeight() functions?

At the moment, the only difference between these two is the text that is displayed to the user. So I suggested creating the promptForInput() function. You pass it a string with the prompt you want displayed, and it also clears out the input buffer. You call it instead of your two ask functions, putting the appropriate string as the parameter.

I want the length and height to be different numbers to do further calculations. But using the one getanswer() function it's not wanting to do that.

Your getAnswer() function reads in an integer from the user. You call it from a function called length() which adds 2 to the length. And you call length() from loop().

You could add another function called height(). It could also call getAnswer() and do any processing specific to height.

Small point, but for consistency, it might be better to call the functions getLength() and getHeight().

Regards

Ray

I would do something like this...

int askInput(char *prompt) {
  Serial.println(prompt);
  return Serial.parseInt();
}

and elsewhere...

height = askInput("Overall height of the Wall");
length = askInput(""Overall length of the Wall");

Also set Serial.setTimeout() to however long you think it would take to enter the numbers in question.

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

void loop()
{
  getHeight();
}

int askInput(char *prompt)
{
  Serial.println(prompt);
  return Serial.parseInt();
}

int getHeight()
{
  int height=askInput("Overall height of the Wall: ");
  return height;
}

int getLength()
{
  int length=askInput("Overall length of the Wall");
}

I tried this, and all it does it just loop "Overall height of the Wall"

DavidOConnor:
Also set Serial.setTimeout() to however long you think it would take to enter the numbers in question.

Serial.setTimeout(500);

500 ms is not enough time to type a number on the keyboard.

I changed it to 10,000 just to make sure.

Now when I enter a number, it just shows the same "Overall height of wall"

I'm still new so try to have some patience haha

void loop()
{
  getHeight();
}

This is basically all your application does right now. Next step is to verify that you can correctly input the height...

void loop()
{
  int height = getHeight();
  Serial.println(height);
}

Then you can move on to create the rest of your awesome application.