Byte to Int Conversion

Hi guys,
I am currently controlling a stepper motor with a potentiometer as part of a project and am expanding it to be able to control from a PC. I will be sending a value equivilent to the pot value (ie upto 1024) but cannot get the value to store properly, I can read each byte into its own variable and spit them out 1 by 1 but cant convert them to an int, ie if I send 743 I can store the 7, 4 and 3 seperatly but cant merge them into a int to pass 743 to the delay.

Heres the code Im using:

if (Serial.available() > 0)
{
SerialSpeed1 = Serial.read();
SerialSpeed2 = Serial.read();
SerialSpeed3 = Serial.read();
SerialTotal = ((SerialSpeed3) + (SerialSpeed210) + SerialSpeed1100);
}

MotorSpeed = (2000/SerialSpeed1)+1;
stepper.step(1);
delay(MotorSpeed);
Serial.print("I received: ");
Serial.print(SerialSpeed1, BYTE);
Serial.print(SerialSpeed2, BYTE);
Serial.println(SerialSpeed3, BYTE);
Serial.print("I received total: ");
Serial.println(SerialTotal);

Any tips would be greatly appreciated
Tom

you need to convert the ascii values into integer values. Try

SerialSpeed1 = Serial.read()- '0'; SerialSpeed2 = Serial.read()- '0'; SerialSpeed3 = Serial.read()- '0';

Thanks thats worked a treat, could you tell me how " -'0 '" does the conversion? Thanks again Tom

http://users.physik.fu-berlin.de/~goerz/blog/wp-content/uploads/2008/09/ascii.gif

http://arduino.cc/en/Reference/Char

Basically a 5 recieved over serial is in reality a char '5' and the equvalent of the numeric value 53 and a '0' is 48, so the code

byte recieved = Serial.read() - '0'; //recieved will be equal to 53 - 48 == 5 when recieving a '5' on the serial

Here is another way of saying the same thing. The ASCII codes for the digits 0 through 9 is 48 through 57. When the character '0' is received it actually has a value of 48. The character '1' has the value 49 etc. You can verify this if you add the following to your sketch:

Serial.print("I received: "); Serial.print(SerialSpeed1, BYTE); Serial.print(” this has an ASCII value of “); Serial.print(SerialSpeed1,DEC);

So to convert from the character '0' (which has a value of 48) to its numeric value you need to subtract '0' which is the same as 48.

Thanks mem.

I am new to Arduino and have been experimenting a bit by making little sketches.

I made sketch which displays a number on a 7 segment display (single digit) received over serial. I created a function with all the combinations of the numbers in it and call the function once Serial.available > 0.

By using Serial.print(x) I could see that 0 was printed as 48 etc.. but using (x,BYTE) displayed the inputted digit.

i tried using int(x) to convert to int as per reference material ut it did not work.

Now I need to expand this to be able to show a 3 digit no. on 3 x 7 segments using multiplexing! :-?

Anyway thanks again.

Very ingenious :)

Be aware that this:

if (Serial.available() > 0)                             
{ 
       SerialSpeed1 = Serial.read();                
       SerialSpeed2 = Serial.read();                
       SerialSpeed3 = Serial.read();

may not always work - you're testing to see if there is at least one character available, then reading three. If the second and third characters haven't arrived yet, then "Serial.read" will return -1.