Datasheet

Chapter 7 Navigating with Infrared Headlights
226Robotics with the BOE Shield-Bot
Testing the System
There are quite a few components involved in this system, and this increases the likelihood
of a wiring error. That’s why it’s important to have a test sketch that shows you what the
infrared detectors are sensing. Use this sketch to verify that all the circuits are working
before unplugging the BOE Shield-Bot from its programming cable.
Example Sketch TestBothIrAndIndicators
Reconnect the programming and power cables, and set the Power switch to 1.
Create, save, and run the sketch TestBothIrAndIndicators.
Verify that the speaker makes a clear, audible tone after the sketch loads.
Use the Serial Monitor to verify that the Arduino still receives a zero from each IR
detector when an object is placed in front of it.
Verify that when you place an object in front of the left IR detector, the left (pin 8)
red LED lights up.
Repeat that test for the right IR detector and pin 7 LED.
/*
* Robotics with the BOE Shield TestBothIrAndIndicators
* Test both IR detection circuits with the Serial Monitor. Displays 1 if
* the left IR detector does not detect an object, or 0 if it does.
* Also displays IR detector states with indicator LEDs.
*/
void setup() // Built-in initialization block
{
tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone
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); // Indicator LEDs
Serial.begin(9600); // Set data rate to 9600 bps
}
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
digitalWrite(8, !irLeft); // LED states opposite of IR
digitalWrite(7, !irRight);
Serial.print(irLeft); // Display 1/0 no detect/detect
Serial.print(" "); // Display 1/0 no detect/detect
Serial.println(irRight); // Display 1/0 no detect/detect