It happens to everyone - you write a sketch which successfully compiles and uploads, but you can't figure out why it's not doing what you want it to. Larger computers have screens, keyboards, and mice that you can use to debug your code, but tiny computers like the RedBoard have no such things.
The key to visibility into a microcontroller is output. This can be almost anything, including LEDs and buzzers, but one of the most useful tools is the serial monitor. Using
Serial.print()
and
println()
, you can easily output human-readable text and
data from the RedBoard to a window back on the host computer. This is great for your sketch's final output, but it's also incredibly useful for debugging.
Debugging your sketches using the Serial Monitor:
Component:
Image Reference:
i20
i24
Servo
e1
e3
e2
e1
Jumper Wire
i19
Jumper Wire
h24
+
Jumper Wire
h24
+
Jumper Wire
h24
+
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
e2
Jumper Wire
e3
Jumper Wire
Flex Sensor
h19
h20
10K
Ω
Resistor
j20
A0
b1
+
a2
+
a3
Pin 9
5V5V
+
GND
And if you run the code again, you'll see the output you wanted:
for
(x =
1
; x <
9
; x++)
{
Serial.print
(x);
}
12345678
You wanted 1 to 8, but the loop is actually giving you 0 to 7. Whoops! Now you just need to fix the loop.
01234567
Let's say you wanted a
for()
loop from 1 to 8,
but your code just doesn't seem to be working right. Just add
Serial.begin(9600);
to your
setup()
function, and add a
Serial.print()
or
println()
to your loop:
for
(x = 0; x < 8; x++)
{
Serial.print
(x);
}
Page 55
Summary of Contents for RedBoard
Page 13: ...Page 11...