ArdBot II in the serial monitor window, the code includes
Serial.println statements to serve
in simple debugging.
LISTING 2. ardbot_sound continued.
//Add slight pause between notes
int pauseBetweenNotes = toneDuration 1.30;
noTone(13); // Stop tone
// From here down copy from Listing 1
// Motion routines and Interrupt handlers
points for the
You might be wondering why the code to control the
servos isn’t in the interrupt handlers. The reason is this: The
delay statement — which is used to steer the robot around
an obstacle — is disabled while processing an interrupt.
Feedback is when your robot
tells you something about what it’s
doing. One form of feedback is the
Our interrupt code merely sets a “flag” variable that
indicates its corresponding switch has been triggered.
This flag is acted upon the next time the loop() code is
repeated. It’s also considered better programming practice
to not place time-intensive functionality within interrupt
Refer back to Part 2 for details on adding a piezo
speaker to your Arduino. Figure 2 provides a reminder of
the connections. Since the piezo element doesn’t need an
amplifier (at least we’re not using one with the ArdBot II),
hookup is quick and easy — just two wires to the Arduino’s
D12 and D13 I/O pins.
The code to steer the bot in a new direction when it
hits something is handled in the loop() function inside two
if statements. If either the pbLeft or pbRight flag variables
have been set, the robot backs up, does a quick about-face
spin turn, then heads off in a new direction.
Before continuing, the flag variable is reset for the next
time. In case you want to monitor the activity of your
Important! As noted in Part 2 of this series, you need
to use a piezo speaker, not a standard dynamic speaker —
one that has a magnet and voice coil. Those kinds of
speakers can draw too much current from the Arduino’s
output pins. Also, be sure to use a piezo speaker (capable
of producing multiple tones) and not a piezo buzzer.
Listing 2 demonstrates playing short tones through the
piezo speaker in response to collisions detected by the
robot’s two switches. A different short ditty plays
depending on which switch is struck. Because the piezo
64 SERVO 10.2013