Infrared Sensors |
FREQOUT Pin, Period, Freq1 (, Freq2)
where:
FREQOUT generates one or two "sine waves" using a pulse-width modulation algorithm and output signal filtering. The frequency values are limited, as given in the manual, to 32767 Hz (BS2, BS2e) or 81917 Hz (BS2sx). However, if the output filtering is omitted, higher frequency components are present in the signal as well. It is possible to specify higher frequency values directly to the routine to generate such components.
'{ $STAMP BS2 } ir_detect VAR BIT LOW 7 loop: PAUSE 50 FREQOUT 7, 1, 38500 ir_detect = IN8 IF ir_detect = 0 THEN unbroken DEBUG home, "Beam is broken; object detected. " GOTO loop unbroken: DEBUG home, "Beam is unbroken; object not detected." GOTO loop |
Objects which are close enough to the emitter will reflect some of the IR signal back to the detector. NOTE: dark objects absorb IR signals very well.
'{ $STAMP BS2 } ir_detect VAR BIT LOW 7 loop: PAUSE 50 FREQOUT 7, 1, 38500 ir_detect = IN8 IF ir_detect = 0 THEN not_detected DEBUG home, "Output is high, no object detected." GOTO loop not_detected: DEBUG home, "Output is low, object is detected. " GOTO loop |
The 40 kHz detector has maximal response around a frequency of 38.5 kHz. By progressively "detuning" the emitter away from this value, we can approximate a reflecting object's distance.
'{ $STAMP BS2 } counter VAR NIB ir_outputs VAR BYTE ir_freq VAR WORD OUTPUT 7 main: ir_outputs = 0 ' Load sensor outputs into l_ir_outputs and r_ir_outputs ' using a for...next loop, a lookup table, and bit addressing. FOR counter = 0 TO 4 LOOKUP counter, [37500,38250,39500,40500,41500], ir_freq FREQOUT 7, 1, ir_freq ir_outputs.LOWBIT(counter) = ~IN8 NEXT ' Display l_ir_outputs and r_ir_outputs in binary and ncd format. DEBUG home, "Readings from IR detector", cr DEBUG "Binary IR_outputs: ", bin5 ir_outputs, cr DEBUG "Object is in zone: ", dec5 NCD(ir_outputs) GOTO main |
See the Stamp Weekend Application Kit for an example program that can be used to read messages from a universal remote control.