Sending Integers to arduino using serial monitor

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?

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("");
}
  while(Serial.available()==0)
  {}
  delay(500);

Do nothing until there is serial data to read. As soon as there is, do nothing some more. Why?

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.

...R

Thank you very much

I will try the first solution for subtracting 48

I will try the first solution for subtracting 48

Don't. Subtracting '0' is far more obvious. And does the same thing.

I will try the first solution for subtracting 48

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: http://www.chiark.greenend.org.uk/~sgtatham/putty/) to send the actual integer values and NOT the ASCII values to your Arduino.

You can however use something like puTTY (link: http://www.chiark.greenend.org.uk/~sgtatham/putty/) to send the actual integer values

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.

Enter: SerialLiaison! http://forum.arduino.cc/index.php?topic=220089.0

You can use the Send Byte half of the screen to send the actual number, 0-255. Just make sure you enter the COM port name before you send!

EDIT: In order to use the byte that's sent to the Arduino as in int, you have to save it as one, like so:

int data = Serial.read();

Good luck!