send a command using serial

hi am trying to send a command using serial from adruino uno to control a device. The command is CM dd.
dd is a decimal value
its giving me this error : error: expected `)' before 'DD', how can i send this command .

void setup() { //This function gets called when the Arduino starts
Serial.begin(9600); //This code sets up the Serial port at 9600 baud rate

}

void loop() {
pinMode(9, OUTPUT);

int DD;
DD= temp;
Serial.write( CM DD); command to turn on motor

thanks in advance

You have been rather economical with your explanation so I will have to guess.

It looks like you want to send the characters CM followed by a space followed by a number.

Even if this guess is correct I don't know, and can't guess, whether you want to send the number as a binary value or as its text equivalent. For example you could send the number 118 as three characters or as a single byte with a value of 118 (which happens to be the Ascii code for 'v'.

Assuming you want to send it as a byte this is the code you would need

byte myNum = 118;
Serial.print("CM ");
Serial.write(myNum);

Then there is another uncertainty - do you want to send a carriage return or linefeed or both after it?

...R

@mrburnette - one of us has the direction of communication wrong :slight_smile:

@robin2 apologies for my explanation am completely a begginer in programming , yes ur guess is right i want to send those characters followed by space followed by number can't i send as CM 80 in serial write. i need to send a decimal value, i want to send a line feed, appreciate ur reply

thanks in advance

SRIv:
@robin2 apologies for my explanation am completely a begginer in programming , yes ur guess is right i want to send those characters followed by space followed by number can't i send as CM 80 in serial write.

Why do you think I showed you some code to do the job?

i need to send a decimal value, i want to send a line feed

I know you want to send a decimal value but you have to tell me what format you want to send it in. Decimal just tells me it is base 10 rather than, say, Hex which is base 16. Within the Arduino everything is in binary (base 2).

I gave you an example earlier of the difference between sending a number as text and as binary. Which you use is determined by the program that is receiving the data.

It would also be useful to know the range of values in the number you want to send. A byte can only deal with numbers between 0 and 255.

...R

Robin

@mrburnette - one of us has the direction of communication wrong :slight_smile:

It was I... I deleted response quickly, but not quick enough.

Ray

hi am trying to send a command using serial from adruino uno to control a device. The command is CM dd. dd is a decimal value

If you can change the sending format, then have the value first followed by the designator, like dd cm. Very easy to decode.