# Getting two digit numbers

**URL:** <https://forum.arduino.cc/t/getting-two-digit-numbers/663300>\
**Category:** Programming\
**Created:** [June 30, 2020, 10:05am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300 "2020-06-30T10:05:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon57380292](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@anon57380292](https://forum.arduino.cc/u/anon57380292)\
**Post date:** [June 30, 2020, 10:05am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/1 "2020-06-30T10:05:31Z")

</div>

Hello everyone! I just have a small question to you all. I am not able to receive double digit numbers using Serial.read(). Does anyone know how to receive then and put them in an int variable? Thanks.

---

<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:** [June 30, 2020, 10:58am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/2 "2020-06-30T10:58:42Z")

</div>

Have a look at the examples in [Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0) - simple reliable non-blocking 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:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [June 30, 2020, 3:24pm UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/3 "2020-06-30T15:24:44Z")

</div>

Serial.parseInt() will return an integer. If it doesn't see any digits within a second it returns zero so it's usually best to use "while (Serial.available()== 0){}" first to make sure the value is starting to arrive before calling Serial.parseInt(). If there might be any characters AFTER the number that you don't want mistaken for the next number, "while (Serial.read() != -1){}" before the "while (Serial.available()== 0){}" will flush those out.

---

<div class="post-metadata">

**Author:** ![anon57380292](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@anon57380292](https://forum.arduino.cc/u/anon57380292)\
**Post date:** [July 1, 2020, 1:42am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/4 "2020-07-01T01:42:46Z")

</div>

Ok Thank you all! I actually figured out a good method to receive multiple digits. Hope this will be of help in the future -

```cpp
int getMultiple()
{
  String s;
  int i;

  s = Serial.readString();

  i = s.toInt();
   
  return i;
}

void loop()
{
    while ( !Serial.available() );
    a = Serial.read(); // a is a declared  integer 

    Serial.println ( a );
}

```

But thank you for all your replies! I will check all of them and see which is most efficient!

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [July 1, 2020, 2:07am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/5 "2020-07-01T02:07:31Z")

</div>

Take care using the String class with Arduino boards with small memory. Memory corruption is possible if String are used improperly. See the [Evils of Strings page](https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/) for explanation.

I recommend Robin2's tutorial.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [July 1, 2020, 11:54am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/6 "2020-07-01T11:54:38Z")

</div>

> TheUNOGuy:  
> Ok Thank you all! I actually figured out a good method to receive multiple digits. Hope this will be of help in the future :-
> 
> ```arduino
> int getMultiple()
> 
> ```

{  
&nbsp; String s;  
&nbsp; int i;

s = Serial.readString();

i = s.toInt();  
&nbsp;  
&nbsp; return i;  
}

> ```arduino
> 
> But thank you for all your replies! I will check all of them and see which is most efficient!
> 
> ```

I think this will work just as well and doesn't use String.

```arduino
int getMultiple()
{
  int i = Serial.parseInt();

  // Like Serial.readString().toInt(), throw out any 
  // extra characters that arrive after the number
  // and before the timeout.
  unsigned long startTime = millis();
  while (millis() - startTime < 1000)
    Serial.read();

  return i;
}

```

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [July 1, 2020, 12:09pm UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/7 "2020-07-01T12:09:29Z")

</div>

Hi,  
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".  
OR  
[http://forum.arduino.cc/index.php/topic,148850.0.html](http://forum.arduino.cc/index.php/topic,148850.0.html).  
Then look down to item #7 about how to post your code.  
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... 🙂

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 1, 2020, 2:07pm UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/8 "2020-07-01T14:07:55Z")

</div>

> johnwasser:  
> Serial.parseInt() will return an integer. If it doesn't see any digits within a second it returns zero [...].

Is it within a second or within an adjustable "timeout period" preset by this code: **Serial.setTimeout(arg);**?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 1, 2020, 2:10pm UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/9 "2020-07-01T14:10:17Z")

</div>

> GolamMostafa:  
> Is it within a second or within an adjustable "timeout period" preset by this code: **Serial.setTimeout(arg);**?

The default value is 1000ms, setTimeout() is optional.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 1, 2020, 2:51pm UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/10 "2020-07-01T14:51:47Z")

</div>

> johnwasser:  
> [...] to make sure the value is starting to arrive before calling Serial.parseInt().

Why is "starting to arrive" and not "has arrived"?

**1.** The characters/data bytes that have arrived at the Serial Port (the hardware UART Port) are being collected and saved into "Serial FIFO Buffer" on interrupt basis at the end of sketch uploading.

**2.** When uploading is done, the Serial.parseIn() also begins to work.

**3.** The above two processes are asynchronous; there is no way to tell which one has started first.

**4.** As the parseIn() method looks for charcaters/data bytes into the Serial Buffer, the user must be sure that the Serial Buffer has accumulated at least one character/data byte (edit) ~~data bytes~~ in order to ensure that parseInt() method works correctly. This is done, as you have shown, by the following code:

```arduino
while(Serial.available() == 0)
{
    //wait
}

```

//----------------------------------------------------------------------------------------------------------------  
**5.** The parseInt() method will terminate with zero return value if it does not find a character in the Serial Buffer within the "timeout period" since it has started.

**6.** The parseInt() method will terminate with a non zero return value if it does not find a character in the Serial Buffer within next "timeout period" since it has finished reading one or more valid charcaters (0 to 9).

**7.** The parseInt() method will also terminate if it finds a non-digit character (outside 0 to 9). The return value will be 0 if the very first character so read is a non-digit character. The return value will be non-zero if a non-digit character is encountered after reading one or more valid charcaters (0 to 9).(Edit)  
//----------------------------------------------------------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 1, 2020, 3:31pm UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/11 "2020-07-01T15:31:53Z")

</div>

In scenario #4, 'while(Serial.available() == 0)' does not ensure that all the characters are available, it only ensures that at least one character is available. It's possible that the rest haven't arrived yet, and may or may not arrive before the time out period is exceeded.

For this reason, it can be blocking code.

---

<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:** [July 1, 2020, 4:22pm UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/12 "2020-07-01T16:22:26Z")

</div>

IMHO it would be much better to have

```cpp
if (Serial.available() >= X) {
  myVal = Serial.parseInt();
}

```

where X is the smallest number of characters that you propose to send. If you always send 2-digit numbers then X should be 2

...R

---

<div class="post-metadata">

**Author:** ![ArnavPawarAA](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@ArnavPawarAA](https://forum.arduino.cc/u/ArnavPawarAA)\
**Post date:** [July 1, 2020, 4:25pm UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/13 "2020-07-01T16:25:49Z")

</div>

I would suggest @TheUNOGuy to use char arrays instead of strings.

..Arnav

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [July 2, 2020, 6:37am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/14 "2020-07-02T06:37:29Z")

</div>

> ArnavPawarAA:  
> I would suggest @TheUNOGuy to use char arrays instead of strings.
> 
> ..Arnav

? :o

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 2, 2020, 8:17am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/15 "2020-07-02T08:17:49Z")

</div>

> ArnavPawarAA:  
> I would suggest @TheUNOGuy to use char arrays instead of strings.

**1.**"char array" is an array whose all elements are ASCII coded values. For example:

```arduino
char myArray[ = {'A', 'B', 'C'};
==> char myArray[] = {0x41, 0x42, 0x43, 0x00};

```

**2.**"string" (and not String with Capital S) is a "set of printable characters of the English Language"; these characters could be members of a char-type array.

**3.** (So,) "char array" and "string' are the same things at conceptual/physical level.

**4.**"String" (with Capital S) is a user-defined "data type" which has been created using "class" keyword. An object (variable) could be declared using the said "String" data type. For example:

```arduino
String myString;

```

**5.** The object/variable "myString" contains conglomerate of data items along with methods and functions for the processing of data items; whereas, "myArray' variable contains only one type of data items and no methods/functions.

---

<div class="post-metadata">

**Author:** ![pcbbc](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@pcbbc](https://forum.arduino.cc/u/pcbbc)\
**Post date:** [July 2, 2020, 8:37am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/16 "2020-07-02T08:37:27Z")

</div>

> GolamMostafa:
> 
> ```arduino
> char myArray[] = {'A', 'B', 'C'};
> 
> ```

==\> char myArray[] = {0x41, 0x42, 0x43, 0x00};

> ```arduino
> 
> ```

That's wrong.

No NUL is added if you use character constants...

```arduino
char myArray[] = {'A', 'B', 'C'};
==> char myArray[] = {0x41, 0x42, 0x43 };

```

You'd have to add your own if you wanted a terminating NUL...

```arduino
char myArray[] = {'A', 'B', 'C', 0 };
==> char myArray[] = {0x41, 0x42, 0x43, 0x00 };

```

Or..

```arduino
char myArray[] = "ABC";
==> char myArray[] = {0x41, 0x42, 0x43, 0x00 };

```

---

<div class="post-metadata">

**Author:** ![anon57380292](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@anon57380292](https://forum.arduino.cc/u/anon57380292)\
**Post date:** [July 2, 2020, 9:04am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/17 "2020-07-02T09:04:46Z")

</div>

Hello, it is actually good to use Serial.parseInt() and because I have come from a C++ background I know abut Char Arrays and I will definitely use them. Thank you so much for your support!

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 2, 2020, 9:20am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/18 "2020-07-02T09:20:27Z")

</div>

> Robin2:  
> IMHO it would be much better to have
> 
> ```arduino
> if (Serial.available() >= X) {
> 
> ```

myVal = Serial.parseInt();  
}

> ```arduino
> 
> where X is the smallest number of characters that you propose to send. If you always send 2-digit numbers then X should be 2
> 
> ...R
> 
> ```

just in case CR/LF can come in too and be a left over from the previous communication, you could wait for the first character (peek) to be either a +, - or a digit so that you know parseInt() will get work done and ignore other characters

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 2, 2020, 11:36am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/19 "2020-07-02T11:36:49Z")

</div>

@pcbbc

**1.** From your Post#15, it appears that the following two declarations are not equivalent.

```cpp
char myArray[] = {'A', 'B', 'C'};
char myArray2[] = "ABC";

```

According to your Post#15, a NULL byte (0x00) is not inserted (during compilation) in the TOP declaration; but, it (the NULL byte) is inserted for the BOTTOM declaration.

**2.** As I know (form my DOS/int programming) that a NULL byte is needed at the end of a string so that the "output to console" (equivalent to print()?) command knows the end of the string.

**3.** When print() method is applied for both declarations of Step-1, ABC appears on the Serial Monitor in both cases.

**4.** Moreover, the execution of Serial.print(myArray[3]) for the TOP declaration of Step-1 shows 0 which is essentially a NULL byte.

I am still at the side to accept that the declarations of Step-1 are equivalent.

---

<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:** [July 2, 2020, 11:39am UTC](https://forum.arduino.cc/t/getting-two-digit-numbers/663300/20 "2020-07-02T11:39:35Z")

</div>

> GolamMostafa:  
> I am still at the side to accept that the declarations of Step-1 are equivalent.

Why not write a short program to investigate rather than confusing the OP's Thread?

...R

[Next page](https://forum.arduino.cc/t/getting-two-digit-numbers/663300.md?page=2)
