How to read two characters with integer from serial communication?

I want to read the following commands given to Arduino from serial communication to control two servo motors.

RR120
LR23

First character select the motor, second character select the direction and integers given the position. I'm going to decode this code by if else or switch operation so that I need to read them separately.

I'm going to decode this code by if else or switch operation so that I need to read them separately.

So, what is the problem?

It would be far easier to know when an packet started and ended if you used start and end markers. "" or "".

Then, read Robin2's serial tutorial:
http://forum.arduino.cc/index.php?topic=396450.0

Sorry for the inconvenience. I edited the post.

I'm sending command like RR120 and LR23 .

Probably even a little easier still if the data is in the form <LR,23>

...R

Indeed start by adding start/end tags or how are you going to know whether it's RR10 or RR100? The comma doesn't seem necessary if you always send two alphanumerical characters, then some digits.

to control two servo motors.

First character select the motor, second character select the direction and integers given the position

I don't understand the use of direction. If you are using servos, the direction will be determined by the difference between the last position and the new position. For example, to go from 150 to 50, there is only one way to go unless the servo can rotate through 360 to get to 50.

@thar07, things would be even easier if the command was a single character. Using the upper an lowercase letters gives you 52 options, which is probably enough.

...R

Robin2:
@thar07, things would be even easier if the command was a single character. Using the upper an lowercase letters gives you 52 options, which is probably enough.

To expand further on this: you should be able to send a number as single char, for a range of 0-255. That allows you to set the servo at 2-degree intervals (if your servo has that kind of accuracy, of course) by using 0-179 and then times two. Or a 1-degree resolution if all you need (or your servo can do) is 256 degrees.

wvmarle:
To expand further on this: you should be able to send a number as single char, for a range of 0-255.

I think you mean a byte.

But if the OP goes down that route he cannot use < and > as start and end markers. I would only recommend sending binary data when it is absolutely essential for performance purposes.

...R

Char or byte or uint8_t... still haven't totally figured out the difference - on one hand they seem to be different, on the other hand the compiler says they're aliases.

A char is a signed 8 bit value. A byte is an unsigned 8 bit value. I learned this the hard way when trying to interface an Arduino with a Java program :slight_smile:

...R

Ah, of course.
Unsigned char then.
(with the added linguistic weirdness that a char - which I may assume is short for "character" - can be a negative value the first place).

wvmarle:
(with the added linguistic weirdness that a char - which I may assume is short for "character" - can be a negative value the first place).

It's that sort of nonsense that gets up my nose when the C "experts" tell us how much better "their" language is. :slight_smile:

...R

Robin2:
It's that sort of nonsense that gets up my nose when the C "experts" tell us how much better "their" language is. :slight_smile:

...R

I have never understood the purpose of having signed chars. What does -27 mean, as a character, anyway?