# Serial.read() question

**URL:** <https://forum.arduino.cc/t/serial-read-question/566526>\
**Category:** Networking, Protocols, and Devices\
**Created:** [January 9, 2019, 10:02am UTC](https://forum.arduino.cc/t/serial-read-question/566526 "2019-01-09T10:02:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dacha011](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@Dacha011](https://forum.arduino.cc/u/Dacha011)\
**Post date:** [January 9, 2019, 10:02am UTC](https://forum.arduino.cc/t/serial-read-question/566526/1 "2019-01-09T10:02:20Z")

</div>

Hello,

I cant figure out what is the problem with this code. For now Serial.read() is a bit tricky to me.

```cpp
void setup() {                
   Serial.begin(9600); 
   digitalWrite(5,LOW);
 
  }

 void loop() {
       
      if (Serial.available() > 0)
       {
       delay(20);
       int number= Serial.parseInt();
         
       if (number < 3){
          digitalWrite(5,HIGH);
          Serial.println(number);
          Serial.println("Less then 3");
         }

       else if (number > 3){
         
            digitalWrite(5,LOW);
            Serial.println(number);
            Serial.println("More then 3");          
         }
       }
   
}

```

* * *

When i type a number, everything is ok, but after a second, the var "number" is ZERO 😮 I have no idea why.

If i type in the Serial monitor 5 i get this

5  
More then 3  
0 ????? 😢  
Less then 3 ??????

---

<div class="post-metadata">

**Author:** ![septillion](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@septillion](https://forum.arduino.cc/u/septillion)\
**Post date:** [January 9, 2019, 10:28am UTC](https://forum.arduino.cc/t/serial-read-question/566526/2 "2019-01-09T10:28:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Dacha011](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@Dacha011](https://forum.arduino.cc/u/Dacha011)\
**Post date:** [January 9, 2019, 11:04am UTC](https://forum.arduino.cc/t/serial-read-question/566526/3 "2019-01-09T11:04:58Z")

</div>

Thanks Septilion for the fast replay.  
I have edited the text ,I have code tags now 🙂

"Next, I think I know why you find Serial.read() tricky. That's because you NEVER use it 😉 " - exactly, I'm learning how to use it 🙂

Newline - if that was the question 🙂

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [January 9, 2019, 11:15am UTC](https://forum.arduino.cc/t/serial-read-question/566526/4 "2019-01-09T11:15:15Z")

</div>

Have a look at the examples in [Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0) - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [January 9, 2019, 11:27am UTC](https://forum.arduino.cc/t/serial-read-question/566526/5 "2019-01-09T11:27:03Z")

</div>

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.

> **[Serial.parseInt() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/communication/serial/parseint/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![septillion](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@septillion](https://forum.arduino.cc/u/septillion)\
**Post date:** [January 9, 2019, 2:19pm UTC](https://forum.arduino.cc/t/serial-read-question/566526/6 "2019-01-09T14:19:49Z")

</div>

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 :🙂

---

<div class="post-metadata">

**Author:** ![Dacha011](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@Dacha011](https://forum.arduino.cc/u/Dacha011)\
**Post date:** [January 10, 2019, 9:19am UTC](https://forum.arduino.cc/t/serial-read-question/566526/7 "2019-01-10T09:19:44Z")

</div>

Thanks for the advice, especially about the newline.  
The Serial Input Basic tutorial Robin2 posted is quite good 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 11:13am UTC](https://forum.arduino.cc/t/serial-read-question/566526/8 "2021-05-06T11:13:51Z")

</div>

Closed on May 6, 2021, 11:13am UTC


