The test code displays the state of each IR detector: 1 = no infrared reflection detected, 0 = yes, infrared reflected from an object is detected.
It is essential to make sure that both IR detectors work correctly before continuing.
What the IR Sensors Can't See
Remember, the IR sensor system is looking for reflected infrared light. Shiny, light-colored objects reflect infrared light well, while dull, dark-colored objects absorb infrared light instead of reflecting it. So, if your IR sensors can't see your black shoes or a black plastic wastebasket, don't worry, that is normal.
First, the program makes I/O pins P26 and P27 output low (0 V). Not only does this turn off the P26 and P27 LEDs, but it also connects the D/A0 and D/A1 sockets to 0 V. The IR LED cathodes are connected to these sockets, using them for a ground connection. (We will explain why in a later activity.)
The rest of the code is inside a repeat forever loop. First, a frequency out block generates a 38000 Hz signal on P11, for 1 millisecond. P11 connects to an IR LED, causing it to strobe on/off very rapidly in a 1 ms burst.
This block is followed immediately by a check PIN block, which stores the input state of P10 in a variable named irLeft. P10 connects to the left IR receiver circuit. These IR receivers detect not just the presence of infrared light but are most sensitive to IR light that is strobing on/off very rapidly, at 38 kHz (38000 Hz). The IR detector sends a low signal (0 V) when a reflected 38 kHz IR signal is detected, and a high signal when no such signal is detected.
The process is duplicated for the right-side IR circuitry with another frequency PIN + check PIN block pair, storing the IR receiver's output in the irRight variable.
The next six lines should look familiar by now. The four Terminal print blocks display labels and values for the irLeft and irRight variables. A pause (ms) 150 block slows down the loop for optimal display. Finally, a Terminal clear screen block erases the old data before the next trip through the loop.
Infrared — It means “below red,” and it refers to the fact that the light’s frequency is below that of red on the color spectrum.
Filtering — The IR receiver has a filter built in that makes it look for infrared that flashes on/off 38000 times per second (38 kHz). This allows it to differentiate between infrared coming from the TV remote and other IR sources such as halogen and incandescent lamps as well as sunlight streaming in through a nearby window.
Sunlight — Even though the IR receiver filters for sunlight, direct sunlight can often swamp the IR LED’s signal. Try to keep it indoors and out of any sunlight streaming in through windows.
The P26 and P27 lines are currently in use providing 0 V to the IR LED cathodes, so you cannot use their LEDs for indicating the IR detector states like you did with whiskers. So, let’s build some LEDs on the breadboard for "object detected" indicators.
*P14/P15 resistors are only needed for ActivityBot kits using External Encoders (#32500 [1]).
Next, you will need to modify the code to turn a given light on if its IR receiver returns 0, or off if it returns 1.
If you have an infrared remote, you can further modify your IR detection code so that it sounds an alarm if it detects IR interference from the environment. This is a useful test to find out if nearby fluorescent lights or other devices are sending out signals that could affect your robot's behavior. The key is not to emit any infrared with the IR LEDs. If the receivers still send a low signal, there must be IR interference coming from another source.
If interference is detected, make the piezospeaker get your attention with a series of six 50 ms, 4 kHz chirps separated by six 50 ms pauses. You can use a TV remote to test this.
Now you can disconnect the robot from the computer and walk around your workspace. Try pointing your robot at different light sources. If you hear the beeping alarm, you know that a 38 kHz infrared signal is detected. The ballast on some styles of fluorescent tube lighting is notorious for causing this kind of IR interference.
Links
[1] https://www.parallax.com/product/32500