0

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);
}
  • your code does not compile because of a syntax error – jsotola Jul 12 at 18:53
  • Serial.read() returns a byte ... what that byte means to you, is not realted to that fact – jsotola Jul 12 at 19:00
1

If it's a single digit:

if (t >= '0' && t <= '9') {
    int numeric_value = t - '0';
} else {
    // Handle the input error.
}
| improve this answer | |
0

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);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.