user73341: I am experimenting with the following program I found online (with slight modifications):
int x;
void setup() {
 Serial.begin(9600);
 Serial.setTimeout(1);
}
void loop() {
 while (!Serial.available());
 x = Serial.readString().toInt();
 int y = x + 1;
 Serial.print(y);
}

I am encountering an error in Serial monitor output; if I input 1, I get 21 printed. I think what's happening is that every digit in a number gets incremented by 1, and then 1 is put at the end for some reason. For example, if I input 234, my output is 3451. I think the error lies with the line  x = Serial.readString().toInt();, but am unsure how to fix this. Thank you!

user4143: There's two problems here.
First, you don't enter "234", you enter "2" followed by "3" and finally "4". Your really small timeout doesn't give the digits enough time to arrive.
Second, your line ending is being received and interpreted as "not a number" which is returned as 0 by .toInt().
What you need to do is:

Don't set a short timeout - you can remove that line entirely.
Use .readStringUntil('\n') to read up until your line ending in one go.


73341: <p>I am experimenting with the following program I found online (with slight modifications):</p>
<pre><code>int x;
void setup() {
 Serial.begin(9600);
 Serial.setTimeout(1);
}
void loop() {
 while (!Serial.available());
 x = Serial.readString().toInt();
 int y = x + 1;
 Serial.print(y);
}
</code></pre>
<p>I am encountering an error in Serial monitor output; if I input <code>1</code>, I get <code>21</code> printed. I think what's happening is that every digit in a number gets incremented by 1, and then <code>1</code> is put at the end for some reason. For example, if I input <code>234</code>, my output is <code>3451</code>. I think the error lies with the line <code> x = Serial.readString().toInt();</code>, but am unsure how to fix this. Thank you!</p>
 4143: <p>There's two problems here.</p>
<p>First, you don't enter &quot;234&quot;, you enter &quot;2&quot; followed by &quot;3&quot; and finally &quot;4&quot;. Your really small timeout doesn't give the digits enough time to arrive.</p>
<p>Second, your line ending is being received and interpreted as &quot;not a number&quot; which is returned as 0 by <code>.toInt()</code>.</p>
<p>What you need to do is:</p>
<ol>
<li>Don't set a short timeout - you can remove that line entirely.</li>
<li>Use <code>.readStringUntil('\n')</code> to read up until your line ending in one go.</li>
</ol>
