problem in sending values serailly

hi everyone
i wanted to controll the dc motor speed serially.so i tried to vary voltage of pwm pin serially using this sample sketch

int incomingByte = 0;
// for incoming serial data
int ledpin=9;

void setup() {
Serial.begin(9600);// opens serial port, sets data rate to 9600 bps
pinMode(ledpin,OUTPUT);
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

analogWrite(ledpin,incomingByte);
delay(100);
//digitalWrite(ledPin, LOW);
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
but whenevr i tried to send numbers like 1,2,3..it returns it's ascii values.and if i send 12,13 or any two digit number it takes values by seperating the number
for eg. if i have sent 12 then i serial window i got
i received: 49
i received :50
likewise
i want to send values 0 to 255 so i can vary the dutycycle
wht should be the problem?
thank you in advance

This comes up about once a week.
Have a look around the forum.

but whenevr i tried to send numbers like 1,2,3..it returns it's ascii values.and if i send 12,13 or any two digit number it takes values by seperating the number

Yes that is how it works and that is how it is supposed to work.
You have to deal with what ever format you are receiving, this is raw data you are getting.

just write some piece of coding in a funtion and use it. its pretty simple. perhaps a switch case. I think we need a proper function to deal with this.

I think we need a proper function to deal with this.

No I disagree, you can try and insulate people too much from the face that they are dealing with a micro controller and not a PC.
If you don't have a sense of what these things are you get the stupid requests like "I want to stream video through the arduino and add extra synthesized sound to it"

Why not just send the ASCII byte - 0x00, 0x01, 0x02...0x0A, 0x0B, 0x0C...0xFD, 0xFE, 0xFF?

Those values (in hex) correspond to the values 0, 1, 2...10, 11, 12...253, 254, 255. Just output those characters to the port, and read in the values on the Arduino.

For example, say you wanted the number "65" for the Arduino; ASCII value 65 equals "A" - so send an "A" (66=B, and so on).

For the lower values, you may need to get tricky, depending on the value. For example, "0" is the null character, so in C you would send "\0" (IIRC). For "10", which is the linefeed character, hex 0x0A - you would send "\r". Depending on how you wrote your serial output interface on the PC side, you may be able to get away with sending the hex values directly.

As others have noted, there are plenty of examples on this, and there are plenty of libraries that deal with this issue if that works better for you and you have the leftover memory to include one. You already know you are getting ASCII values for characters you send; just expand on this knowledge (note - it is a good idea in your loop to look for start and end characters that signify the beginning and end of the transmission; that way, spurious characters won't likely cause an unwanted action to occur).

Good luck.

:slight_smile:

thank you guys
i was doing the stupid mistake.i was used to send the integer value.when i tried to send byte value i got the answer cause integer value requires two bytes where in serial buffer is only of one byte i supposed.
correct me if anything seems illogical