I need to send an integer, say between 0-10000, to Arduino using serial communication. What's the best way of doing that?
I can think of breaking the number into an character array (eg: 500 as '5','0','0') and sending them as a byte stream (yeah, that's ugly). Then reconstruct at the other end. (anything is sent serially as a byte stream, right?)
Isn't there a better way? Somehow, it should be able to assign the value into an int
type variable.
(really need to know the same thing about strings too, if possible)
If what you're looking for is speed, then instead of sending an ASCII encoded int, you can divide your number into two bytes, here's an example:
uint16_t number = 5703; // 0001 0110 0100 0111
uint16_t mask = B11111111; // 0000 0000 1111 1111
uint8_t first_half = number >> 8; // >>>> >>>> 0001 0110
uint8_t sencond_half = number & mask; // ____ ____ 0100 0111
Serial.write(first_half);
Serial.write(sencond_half);