Datasheet
Chapter 7 • Navigating with Infrared Headlights
246 • Robotics with the BOE Shield-Bot
Project Solutions
1. One approach would be to add this code at the end of the setup function in
FastIrRoaming. It will wait until an object is detected before allowing the code to
move on to the roaming code in the
loop function.
int irLeft = 1; // Declare IR variables and
int irRight = 1; // initialize both to 1
while((irLeft == 1) && (irRight == 1)) // Wait for detection
{
irLeft = irDetect(9, 10, 38000); // Check for object on left
irRight = irDetect(2, 3, 38000); // Check for object on right
}
2. Rotating in place and waiting to see an object is almost the same as project 1. The
trick is to call the
maneuver function to make it rotate slowly. Remember that
the
maneuver function’s msTime parameter just dictates low long
the
maneuver routine prevents another maneuver from making the BOE Shield-
Bot do something different. You can instead use it to start a maneuver in a set-it-
and forget-it fashion. Then, let the code continue to the loop that monitors the IR
detectors until it detects an object.
int irLeft = 1; // Declare IR variables and
int irRight = 1; // initialize both to 1
while((irLeft == 1) && (irRight == 1)) // Wait for detection
{
irLeft = irDetect(9, 10, 38000); // Check for object on left
irRight = irDetect(2, 3, 38000); // Check for object on right
}
Now, to chase after an object, the if…else if…else statement needs to be
updated so that it goes forward when it sees an object with both sensors. It
should also turn toward an object instead of away from it. Here is a
modified
loop function.
void loop() // Main loop auto-repeats
{
int irLeft = irDetect(9, 10, 38000); // Check for object on left
int irRight = irDetect(2, 3, 38000); // Check for object on right
if((irLeft == 0) && (irRight == 0)) // If both sides detect
{
maneuver(200, 200, 20); // Forward 20 ms, chase object
}