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.
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.
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.
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.
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.
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).