Hardware Reference
In-Depth Information
Upload the sketch to your Arduino, then open the Serial
Monitor by clicking on the icon at the top of the editor, as
shown in Figure 2-9.
Once it's open, type:
r9
Then click send. You should see the ball light up red. Now
try:
Figure 2-9
Arduino toolbar, highlighting the Serial Monitor.
r2g7
The red will fade and the green will come up. Now try:
g0r0b8
Figure 2-10
LED with a ping-pong ball on top to diffuse the light.
The blue will come on. Voila, you've made a tiny serially
controllable lamp!
If the colors don't correspond to the color you type, you
probably bought an LED of a different model than the one
specified above, so the pin numbers are different. You
can fix this by changing the pin number constants in your
sketch.
You don't have to control the lamp from the Serial Monitor.
Any program that can control the serial port can send your
protocol to the Arduino to control the lamp. Once you've
finished the next project, try writing your own lamp con-
troller in Processing.
X
Notice in the program how the characters you type are in single quotes? That's because you're using the
ASCII values for those characters. ASCII is a protocol that assigns numeric values to letters and numbers.
For example, the ASCII value for the letter 'r' is 114. The ASCII value for '0' is 48. By putting the characters
in single quotes, you're programming the Arduino to use the ASCII value for that character, not the character itself. For
example, this line:
brightness = map(inByte, '0', '9', 0, 255);
could also be written like this:
brightness = map(inByte, 48, 57, 0, 255);
because in ASCII, the character '0' is represented by the value 48, and the character '9' is represented by the value 57.
Using the character value in single quotes instead of the actual values isn't essential to make your program run, but it
makes it easier to read. In the first version of the line above, you're using the ASCII characters to represent the values
to map; in the second version, you're using the raw values themselves. You'll see examples in this topic that use both
approaches. For more on ASCII, see "What's ASCII?" on page 54.
 
Search WWH ::




Custom Search