Serial.parseInt
returns 0
when it times out with none or invalid characters received.
As you're loop is waiting for available data befor you call Serial.parseInt
the only explanation is that you're sending something else but numbers. Most likely a linefeed or carriage return from your terminal program or a println function...
You should probably call Serial.peek
to check if the next byte is a number befor you call Serial.parseInt
. If you know for sure that you did not send anything but that single integer and some non-numeric character you can also clear the input buffer befor parsing a possible next integer.
But if you're sending an integer and a termination character you might as well just not use Serial.parseInt
. Read your data into an array until you receive the termination character. Then convert your data to an integer yourself.
Using Serial.parseInt
only makes sense if you know you're about to receive an integer representation within a certain time interval.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…