Hi im working on a project where i type in a number and an led turns on and off depending on what it is.
I decided to print the number you type using Serial.print but it will return an Ascii in stead of the number.
example I type in 1 , the screen returns 49
I type in 1001
the screen returns
49
48
48
49
Please help so it returns the number I type
Just subtract 48.
This might be worth a read.
I decided to print the number you type using Serial.print but it will return an Ascii in stead of the number.
Yes that is the way computers work. You can't change it, only manipulate the ASCII code into something that you need like manor_royal told you.
Personally I would AND the number with a mask of 0x0F
number = ASCIIvalue & 0x0F;
In that way you always get a number of 0 to 15 what ever you type, so there is no danger of negative numbers.
Arduino_Matrix_guy:
I type in 1001
the screen returns
49
48
48
49
That's because 49 is the ascii value for 1 and 48 is the ascii value for 0.
If you want to change the character string "1001" into the number 1001 you need to use the atoi() function. "atoi" = ascii to integer.
Have a look at the parse example in Serial Input Basics - simple reliable ways to receive data.
...R
Arduino_Matrix_guy:
Hi im working on a project where i type in a number and an led turns on and off depending on what it is.
I decided to print the number you type using Serial.print but it will return an Ascii in stead of the number.
example I type in 1 , the screen returns 49
I type in 1001
the screen returns
49
48
48
49
Please help so it returns the number I type
All of your program (input, processing, output) is under full control of the program.
The program executes always what the program says.
If you tell Serial.print , the program does a Serial print and possibly converts a single byte or char into decimal ASCII.
Perhaps you want to do Serial.write() instead of Serial.print()?