Hardware Reference
In-Depth Information
communication to talk to the Ethernet controller means
you can still use the serial port to get messages about
what's going on—as you saw in the previous example.
Following are a few tips for effective serial debugging of
networking code using serial messages.
code, they slow it down. The microcontroller has to save
each bit of information received, which takes time away
from your program. Even though the time is miniscule
for each bit, it can add up. After a few hundred bytes, you
might start to notice it. So, make sure to take out your
serial transmission statements when you're done with
them.
It's important to keep in mind that serial communication
takes time. When you have serial statements in your
One way to manage
your debugging
is to have a variable that changes the
behavior of your program, like this.
When every debugging statement is
preceded by the if (DEBUG) condi-
tional, you can easily turn them all off
by setting DEBUG to false.
const boolean DEBUG = true;
Debug It
void setup() {
Serial.begin(9600);
}
void loop() {
if (DEBUG) Serial.println("this is a debugging statement");
}
In the Air-Quality
Index client project,
you had a lot of different methods in
your code. It's not easy to tell whether
every method is being called, so
during troubleshooting, make it a habit
to "announce" serially when every
method is being called. To do so, put a
serial print statement at the beginning
of the method, like the code shown on
the right.
Announce It
void connectToServer() {
if (DEBUG) Serial.print("running connectToServer()...");
// rest of the method goes here
}
void makeRequest() {
if (DEBUG) Serial.print("running makeRequest()...");
// rest of the method goes here
}
boolean setMeter(int thisLevel) {
if (DEBUG) Serial.print("running setMeter()...");
// rest of the method goes here
}
Check Conditionals
It's
common
in a communications application like
these to have multiple conditions that
affect a particular result. As you're
working, you can easily make mistakes
that affect these nested if statements.
As you work, check that they're still
coming true when you think they are by
announcing it, as shown here.
if (client.connected()) {
if (DEBUG) Serial.print("connected");
if (!requested) {
if (DEBUG) Serial.print("connected, not requested.");
requested = makeRequest();
}
else {
if (DEBUG) Serial.print("not connected");
}
}
 
Search WWH ::




Custom Search