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'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?
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?
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
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.
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
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().
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"