First, please edit your post to include code tags. See How to use the forum please
Next, I think I know why you find Serial.read() tricky. That's because you NEVER use it You use the (stupid) .parseInt() method. Do know this function is a blocking function aka will cause your code to pause.
And now the bonus question, to what is the line ending set in Serial Monitor?
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
after the number some control characters are send (line end characters \r and/or 'n). parseInt() waits for characters a second (so delay(20) is for nothing). if it can't read a number, it returns 0.
quote author=Dacha011 link=msg=4010964 date=1547031898]
I have edited the text ,I have code tags now
[/quote]
Thanks!
Dacha011:
exactly, I'm learning how to use it
For that, indeed have a look at Robin2's fine explanation Which is indeed a wayyyyy better way to do it.
Dacha011:
Newline - if that was the question
I Thought that would be the case So after everything you enter a newline is send. .parseInt() read up to (not inclusive) that newline. So after .parseInt() returns the Serial buffer is still filled with a newline (aka Serial.available() still return >0). So a new .parseInt() is started, ignores every non-digit and times out because there is no digit which returns 0
PS "greater than" Yes, I make the same mistake quite often as well :