Datasheet

Navigating with Infrared Headlights • Chapter 7
Robotics with the BOE Shield-Bot 247
else if(irLeft == 0) // If only left side detects
{
maneuver(-200, 200, 20); // Left toward object 20 ms
}
else if(irRight == 0) // If only right side detects
{
maneuver(200, -200, 20); // Right toward object 20 ms
}
else // Otherwise, no IR detects
{
maneuver(200, 200, 20); // Forward 20 ms
}
}
The example above goes forward for 20 ms if it does not see the object. Optionally,
it could spin in place until it sees an object and then chase after it again. For this,
you could place the code that spins and waits for a detection in a function. Then,
your code could call that function from the end of the
setup function to wait until it
sees something, and also from the
else statement in the loop function for when it
can’t see the object any more.
3. The key to solving this problem is to combine FastIrRoaming and the
tone portion
of IrInterferenceSniffer in a single sketch. This example also turns on the lights
while it sounds the alarm. One note, the compiler in the Arduino software won’t let
you declare a variable twice in the same code block. So, in the
loop function, the
first time the
irLeft and irRight variables are used, they are declared with int.
The second time within the loop, they are just reused for detection (without
using
int to declare them) because they have already been declared.
Modified
setup and loop functions are shown here:
void setup() // Built-in initialization block
{
pinMode(10, INPUT); pinMode(9, OUTPUT); // Left IR LED & Receiver
pinMode(3, INPUT); pinMode(2, OUTPUT); // Right IR LED & Receiver
pinMode(8, OUTPUT); pinMode(7, OUTPUT); // LED indicators -> output
tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone
servoLeft.attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach right signal to pin 12
}
void loop() // Main loop auto-repeats
{
// Sniff
int irLeft = digitalRead(10); // Check for IR on left
int irRight = digitalRead(3); // Check for IR on right