can look like a fault in the individual
circuits when it’s really just a matter
of overtaxing the battery. In cases like
these, using a bigger battery or
completely eliminating circuits that
draw excessive currents usually solves
this problem.
boolean ledState = false;
Listing 2 — Debugger.ino
void setup() {
Serial.begin (9600); //Match in Serial Monitor
delay(250);
Serial.println("Setup complete!");
}
Using the
Serial Monitor to
Debug Problems
void loop() {
digitalWrite(13, ledState);
Serial.print("LED on: ");
Serial.println(ledState);
ledState = !ledState;
delay(500);
}
// LED on or off
// LED state in
// Serial Monitor
// Shows 0 or 1
// Toggle LED state
// Wait 1/2 second
Get into the habit of validating
the operation of your robot and its
Arduino processor by using the Serial
Monitor window. This tool allows you
to send messages from the Arduino
back to your PC. These messages
serve as a way to quickly and
efficiently debug many aspects of
your robot/Arduino — all at the same
time. While you must take a few
moments to set the debugging
environment, it can save considerable
time in the long run.
No doubt you’ve already used the
Serial Monitor to display messages
from the Arduino, so you already
know the basic concepts. Just in case
the idea is new to you, here’s what’s
involved:
Serial.begin(9600);
some place within setup(), preferably
near the beginning. The 9600 value is
the baud rate — the data rate that the
Arduino and PC will communicate
with one another. This value is actually
rather low, and both your computer
and Arduino are capable of talking to
one another at much faster speeds.
However, 9600 baud is the most
common rate used in the various
Arduino examples.
Good places to insert a Serial.print
statement include:
Inside the setup() function to verify that the
Arduino is progressing to at least that point in its
program. The Serial.print statement must appear
after Serial.begin. If printing text immediately after
Serial.begin, consider adding a quarter to half
second delay:
Serial.begin(9600);
delay (500); // Wait half a second
Serial.println(“Setup!”);
Inserting a debug statement inside setup() also helps
you to verify that the Arduino runs the setup code just
once. If you see the “Setup!” message repeat, it could
mean your Arduino is being reset over and over again.
This can occur, for instance, if the Arduino is not getting
enough voltage to operate reliably.
At the start and/or end of the loop() function to
check that the Arduino is properly completing each loop.
You can use static text, as in:
Serial.println(“Looping!”);
or set up a more elaborate system where you indicate the
number of loops that have elapsed so far:
60 SERVO 10.2012
int counter = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print (“Loop: “);
Serial.println(counter++);
// rest of code
}
Be careful when using debug statements in the
loop() to prevent over-running the communications
between the Arduino and your PC. If the loop doesn’t
contain programming statements that require at least
half a second to execute, (temporarily) slow things by
inserting a delay at the bottom of the loop:
delay(500);
// Wait half a second
// before proceeding
Inside a user-defined function to verify that the
function is being called. User-defined (your own)
functions are a handy way to execute the same code
whenever a certain event occurs. A good example is
making the robot stop, back up, turn, and head off in a
new direction whenever one of its touch sensor switches
is activated. You can verify that the Arduino is reaching
the user-defined function simply by adding a debugging
statement as the first program line within it: