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);
}
Edgar Bonet
45.1k4 gold badges42 silver badges81 bronze badges
asked Jul 12, 2020 at 11:23
2 Answers 2
If it's a single digit:
if (t >= '0' && t <= '9') {
int numeric_value = t - '0';
} else {
// Handle the input error.
}
answered Jul 12, 2020 at 12:18
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);
}
lang-cpp
Serial.read()
returns a byte ... what that byte means to you, is not realted to that fact