Hardware Reference
In-Depth Information
Example 3-3. The Arduino fade example with serial
int
led
=
9
;
int
brightness
=
0
;
int
fadeAmount
=
5
;
void
setup
()
{
pinMode
(
led
,
OUTPUT
);
Serial
.
begin
(
9600
);
//
}
void
loop
()
{
analogWrite
(
led
,
brightness
);
brightness
=
brightness
+
fadeAmount
;
if
(
brightness
==
0
||
brightness
==
255
)
{
Serial
.
print
(
"Brightness is at "
);
//
Serial
.
print
(
brightness
);
//
Serial
.
println
(
". Switching directions."
);
//
fadeAmount
=
-
fadeAmount
;
}
delay
(
30
);
}
Opens serial port and sets data rate to 9600 bits per second (also
known as the baud rate).
Send a string of text over serial.
Send the value of brightness over serial.
Send a string of text over serial and then a carriage return.
After uploading the code from
Example 3-3
to the board, click the magnifying
glass button on the right side of the Arduino IDE toolbar to open the serial
monitor (as shown in
Figure 3-14
).