Datasheet
Chapter 7 • Navigating with Infrared Headlights
222 • Robotics with the BOE Shield-Bot
The tone actually lasts about 1.1 ms. Even though we would expect tone(9, 38000, 8) to
generate a 38 kHz signal for 8 ms, the signal actually only lasts for about 1.1 ms as of Arduino
software v1.0. Given the name tone, the function may have been designed for and tested with
audible tones. If played on a speaker, a 38 kHz tone will not be audible. It’s in the ultrasonic
range, meaning it’s pitch is so high that it’s above the audible range for humans, which is about 20
Hz to 20 kHz.
The tone function generates a square wave that repeats its high/low cycle 38000 times per
second. Through experimentation with Arduino software v1.0, the author discovered that a
call to
tone with a duration of 8 ms resulted in a 38 kHz square wave that lasted about 1.1
ms.
Remember that the
tone function does its processing in the background. Since the IR
receiver needs a few tenths of a millisecond to respond to the 38 kHz signal,
delay(1)
prevents the
ir = digitalRead(10) call from copying the IR receiver’s output until it’s
ready.
The next sketch defines a function named
irDetect with three parameters: one to specify
the IR LED pin, one to specify the IR receiver pin, and one to set the frequency for flashing
the IR LED.
int irDetect(int irLedPin, int irReceiverPin, long frequency)
In the loop function you’ll see a call to irDetect:
int irLeft = irDetect(9, 10, 38000); // Check for object
This call passes 9 to the irLedPin parameter, 10 to irReceiverPin, and 38000 to
the
frequency parameter. The function performs those three steps for infrared detection
and returns 1 if no object is detected, or 0 if an object is detected. That return value gets
stored in
irLeft.
Example Sketch: TestLeftIr
This sketch only tests the BOE Shield-Bot’s left IR detector. This helps simplify trouble-
shooting because you are focusing on only one of the two circuits. This is yet another
example of subsystem testing. After the subsystems check out, we can move to system
integration. But first, you’ve got to make sure to test and correct any wiring or code entry
errors that might have crept in.
Reconnect the battery pack to the Arduino.
Set the 3-position switch to position 1.
Create, save, and run the sketch the sketch TestLeftIr.