user67578: I want Serial.read() to return an integer instead of an ASCII value. For example, in the below code if I input 2, I want the output as 2 instead of 50
void setup(){
    Serial.begin();
}

void loop(){
    if(Serial.available()){
    int t = Serial.read();
    Serial.println(t);
}


user7508: If it's a single digit:
if (t >= '0' && t <= '9') {
    int numeric_value = t - '0';
} else {
    // Handle the input error.
}


user35097: You can use an alternate method like this
void setup()
{
Serial.begin(9600);
}
void loop()
{
 if(Serial.available()){
    String a = Serial.read();
    int b = a.toInt();
    Serial.println(b);
}


67578: <p>I want Serial.read() to return an integer instead of an ASCII value. For example, in the below code if I input 2, I want the output as 2 instead of 50</p>
<pre class="lang-cpp prettyprint-override"><code>void setup(){
    Serial.begin();
}

void loop(){
    if(Serial.available()){
    int t = Serial.read();
    Serial.println(t);
}
</code></pre>
 7508: <p>If it's a single digit:</p>
<pre class="lang-cpp prettyprint-override"><code>if (t &gt;= '0' &amp;&amp; t &lt;= '9') {
    int numeric_value = t - '0';
} else {
    // Handle the input error.
}
</code></pre>
 35097: <p>You can use an alternate method like this</p>
<pre><code>void setup()
{
Serial.begin(9600);
}
void loop()
{
 if(Serial.available()){
    String a = Serial.read();
    int b = a.toInt();
    Serial.println(b);
}
</code></pre>
