The output looks as follows when you send the character A again:
Unknown command: 65
41
101
1000001
A
Depending on the format specifier,
Serial.println
automatically converts a byte
into another representation. DEC outputs a byte as a decimal number, HEX
as a hexadecimal number, and so on. Note that such an operation usually
changes the length of the data that get transmitted. The binary representation
of the single byte 65 needs 7 bytes, because it contains seven characters.
Also note that we have to use
Serial.write
instead of
Serial.println
to output a
character representation of our command value. Former versions of the
Arduino IDE had a
BYTE
modifier for this purpose, but it has been removed
in Arduino 1.0.
Numbering Systems
It’s an evolutionary accident that 10 is the basis for our numbering system.
If we had only four fingers on each hand, it’d be probably eight, and we’d
probably have invented computers a few centuries earlier.
For thousands of years, people have used denominational number systems,
and we represent a number like
4711
as follows:
4×10
3
+ 7×10
2
+ 1×10
1
+ 1×10
0
This makes arithmetic operations very convenient. But when working with
computers that interpret only binary numbers, it’s often good to use number-
ing systems based on the numbers 2 (binary), 8 (octal), or 16 (hexadecimal).
The decimal number
147
can be represented in octal and hexadecimal as:
0223=3×8
0
+2×8
1
+2×8
2
0x93=3×16
0
+9×16
1
In Arduino programs, you can define literals for all these numbering systems:
int decimal = 147;
int binary = B10010011;
int octal = 0223;
int hexadecimal = 0x93;
Binary numbers start with a B character, octal numbers with a 0, and hex-
adecimal numbers with 0x. Note that you can use binary literals only for
numbers from 0 to 255.
report erratum • discuss
Using Serial Ports • 31
www.it-ebooks.info