I want to send integers to the arduino through the serial monitor but the arduino receives the ASCII code of the integer instead of the integers itself
so when i Write the integer 2 and click send in the serial monitor , the arduino actually receives the number 50 ( ASCII code of 2) instead of simply receiving the integer 2
Ahmed_Samir:
when i Write the integer 2 and click send in the serial monitor , the arduino actually receives the number 50 ( ASCII code of 2) instead of simply receiving the integer 2
How do you know it does that? If I use the code below and hit the 2-key, what I see is exactly what I expect to see, but I would have no idea if ASCII 50 was involved at all.
/* This is a simple test for two way traffic via bluetooth
but you can try it first using the USB cable to the serial
monitor without the bluetooth module connected.
Note that some bluetooth modules come set to a different baud rate.
This means you need to alter the command
Serial.begin(9600) accordingly
Same goes for the setting in the bottom right corner on the
serial monitor */
void setup()
{
Serial.begin(9600);
Serial.println("OK then, you first, say something.....");
Serial.println("Go on, type something in the space above and hit Send,");
Serial.println("or just hit the Enter key");
}
void loop()
{
while(Serial.available()==0)
{}
delay(500);
Serial.println("");
Serial.println("I heard you say:");
while(Serial.available()>0)
{
Serial.write(Serial.read());// note it is Serial.WRITE
}
Serial.println("");
}
Ahmed_Samir:
I want to send integers to the arduino through the serial monitor but the arduino receives the ASCII code of the integer instead of the integers itself
so when i Write the integer 2 and click send in the serial monitor , the arduino actually receives the number 50 ( ASCII code of 2) instead of simply receiving the integer 2
any suggestions for solution?
If you are using the Serial monitor you are not actually writing the integer 2 - you are typing the ASCII character '2' which is why the Arduino correctly receives 50.
There are two possibilities. You can get the Arduino to subtract 48 (the Ascii code for '0') from the numbers you type. That will get you the digits 0 to 9 as integers (and strange stuff if you enter letters). You can also write Arduino code that will accept several digits ('123') and convert them into a single integer.
The other option is to write a PC program (perhaps using Python) to send integers directly to the Arduino. However the communication between the PC and the Arduino will be in bytes so your Arduino code will still need to join pairs of bytes into an integer.
Don't. Subtracting '0' is far more obvious. And does the same thing.
But subtracting a '0' won't work for multi-digit numbers like 25 or 259 or something bigger.
You can however use something like puTTY (link: PuTTY: a free SSH and Telnet client) to send the actual integer values and NOT the ASCII values to your Arduino.
If you send larger than byte sized values, you need to know what order the bytes are sent in, so you can assemble the bytes back in the proper order. This is more challenging that reassembling a value from a string.
What an astounding coincidence! I just recently had that problem myself. Sure, you can subtract 48 from the input, but that only works if you type a one digit number. Because I just recently got Micrsoft Visual Studio 2010, I figured I'd make a program for it.