Hi guys,
Very new to arduino. So I'm trying to gather information from the user. But everytime, I input a number, a zero shows in the next line, and completely skip over the second users input.
I have tried everything here, Lots of youtube videos use the same code as mine.
I don't know how to fix this.
Hope someone can help me out here.
Thanks.
int x = 0 ;
int y = 0 ;
int delaytime = 500;
void setup() {
Serial.begin(9600);
}
void loop() {
inputx();
while(Serial.available() == 0 ){ } // wait until user input a number
inputy();
}
void inputx(){
Serial.println ("input X destination: ");
while(Serial.available() == 0 ){ } // wait until user input a number
if (Serial.available() > 0){
x = Serial.parseInt();
Serial.print(x);
}
}
void inputy(){
Serial.println ("input Y destination: ");
while(Serial.available() == 0 ){ } // wait until user input a number
if (Serial.available() > 0){
y = Serial.parseInt();
Serial.print(y);
}
}
ddatest.ino (764 Bytes)
Line endings in the serial monitor?
Robin2 has an excellent thread in serial input basics.
Serial Input Basics - simple reliable ways to receive data.
...R
After I change from new line to no line ending, it works just fine.
Thanks
@OP
Your program works satisfactorily/unsatisfactorily, and it is evident from the following screenshot.

1. I have uploaded your sketch in my UNO.
2. I have entered the number 1234 from the InputBox of the Serial Monitor and then clicked on the Send button. I have chosen 'No line ending' option in the 'Line ending tab'. The number has been received by the UNO and echoed back in the OutputBox of the Serial Monitor.

3. But why has the sketch behaved odd in your case as depicted below in the screenshot? Most probably, you entered non-digit in the InputBox of the Serial Monitor.

4. If I choose Newline option (which you have probably chosen) in the Line ending tab, then enter 1234 in the InputBox -- I get the following response.

5. If you are happy with the fact that your problem has been solved by choosing 'No line ending' option in the 'Line ending tab', it is fine. If you are still thinking about the roles of the 'New line' and 'No line ending' options in your program, you need to play a little bit with a simplified version of your sketch. Moreover, you may be interested to know why the program does not work when non-digit(s) are entered from the InputBox of the Serial Monitor. The Forum Members might be interested to answer your such questions/queries if any.




martin_cui:
After I change from new line to no line ending, it works just fine.
No! The program does not work!

