# Serial read and basic math

For a speed control exercise I have to read in a value from the serial port that then correlates to the motor speed. The guideline is to take a read from 0-9 that translates to 0-255 PWM. I have the basic structure running but the serial print starts at 1360, not 0, and increments up properly from there. Trying a few combinations of var types and setting them to 0 doesn’t correct this. What might be causing this issue? Code below.

``````void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(6,OUTPUT);//ENA on H-Bridge
pinMode(7,OUTPUT);//EN1 on H-Bridge
pinMode(8,OUTPUT);//EN2 on H-Bridge
pinMode(13,OUTPUT);
}

void loop() {
int input_speed;
int output_speed;
if (Serial.available() > 0) {    // is a character available?
input_speed = Serial.read();       // get the number

// check if a number was received
if ((input_speed >= '0') && (input_speed <= '9')) {
Serial.print("Speed selected: ");
output_speed = (int)input_speed * 28.34;
Serial.println(output_speed);
}
else {
Serial.println("Not a number.");
}
} // end: if (Serial.available() > 0)

}
``````

Zero is ASCII 48. 48 * 28.34 = 1360 Go from there.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

…R

I read through this a little bit, but as a beginner to Arduino it’s difficult to understand without descriptor comments for the syntax.

I’m sorry. Did I miss something?

You are testing against ‘0’ and ‘9’. Once you use the single quotes you are directing the compiler to treat the enclosed values as ASCII characters so in fact you are testing against 48 thru 57 which is why your print starts at 1360. Serial.read loads ASCII characters. If you want integers you have to subtract 48!

DKWatson: I'm sorry. Did I miss something?

You are testing against '0' and '9'. Once you use the single quotes you are directing the compiler to treat the enclosed values as ASCII characters so in fact you are testing against 48 thru 57 which is why your print starts at 1360. Serial.read loads ASCII characters. If you want integers you have to subtract 48!

That makes sense. For the level that I'm at this works.

I did see the parse example from Robin, I'm just not confident in using it without understanding first.

Thanks, guys!

scotdani:
I did see the parse example from Robin, I’m just not confident in using it without understanding first.

If there is something specific you don’t understand please tell me what it is and i will try to explain.

I notice that I posted my link (in Reply #2) at 17:50 (my time) and you posted your Reply #3 at 18:06 or 16 minutes later. That is certainly not enough time in which to learn Serial Input Basics. Take a number of hours and give it some careful study.

…R