The test code displays the state of each IR detector: 1 = no infrared reflection detected, 0 = yes, infrared reflected from an object is detected. Before continuing, make sure that both sides can reliably detect objects.
It's important to make sure both IR detectors work properly before continuing.
What the IR Sensors Can't See
Remember, the IR sensor system is looking for reflected infrared light. Light-colored objects reflect infrared light well, while 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, int variables irLeft and irRight are declared to hold the IR detector output states. Then, inside main, the code sets P26 and P27 low to make sure that the D/A terminals are both providing 0 V to the IR LED cathodes.
The IR receivers are designed to only send detect signals if they see infrared light that flashes on/off at 38,000 times per second (38 kHz). So, in the main function’s while loop, freqout(11, 1, 38000) sends sends high/low signals that repeat 38000 times per second to the IR LED’s anode. This makes it flash on/off at that rate.
Next, irLeft = input(10) saves the IR receiver’s output in the irLeft variable. If the IR receiver’s output is high, meaning no IR reflection detected, input(10) returns a 1. If the receiver’s output is low, meaning reflected IR was detected, intput(10) returns a 0. The same process repeats for the right IR detector with freqout(1, 1, 38000) and irRight = input(2).
/* Test IR Detectors.c */ #include "simpletools.h" // Include simpletools int irLeft, irRight; // Global variables int main() // main function { low(26); // D/A0 & D/A1 to 0 V low(27); while(1) // Main loop { freqout(11, 1, 38000); // Left IR LED light irLeft = input(10); // Check left IR detector freqout(1, 1, 38000); // Repeat for right detector irRight = input(2); print("%c irLeft = %d, irRight = %d", // Display detector states HOME, irLeft, irRight); pause(100); // Pause before repeating } }
After the two infrared detection results are stored in irLeft and irRight, print(%c irLeft = %d, irRight = %d”, HOME, irLeft, irRight) does several things. The %c makes it send the HOME value (1) to SimpleIDE Terminal. That sends the cursor to the top-left position. After that, irLeft = %d, irRight = %d displays irLeft = followed by the 1 or 0 that irLeft stores, then irRight = followed by the 1 or 0 that irRight stores. Last, pause(100) keeps the rate that the SimpleIDE Terminal is updated down in the 10 times per second neighborhood.
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 we cannot use their LEDs for indicating the IR detector states like we did with whiskers. So, let’s build some LEDs on the breadboard for indicators.
*P14/P15 resistors are only needed for ActivityBot kits using External Encoders (#32500).
Next, modify the code to turn a given light on if its IR receiver returns 0, or off if it returns 1.
You can further modify your IR detection code from Try This 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 are sending out signals that are interfering with your detector. The key is to not 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.