Datasheet

Page 80ยท Applied Robotics with the SumoBot
If the IR receiver is sending 5 V to P11, it means it doesn't see any reflected infrared. If
this is the case, the BASIC Stamp stores the value 1 in the P11 input register bit
IN11. If
the IR receiver does detect infrared reflected off some object, it will send 0 V to P11.
While P11 senses 0 V,
IN11 stores a 0. The problem is, the IR receiver only sends that 0
V for a fraction of a millisecond after the
FREQOUT command stops. After the IR
receiver's output rebounds to 5 V,
IN11 stores a 1 again. That's why the command irLF
= IN11
must come immediately after the FREQOUT command. Even though IN11 will
return to 1 by the time the next PBASIC command gets executed, the value 0 gets safely
stored in the
irLF (short for infrared-left-front) variable for as long as the program needs
it.
A more formal way to write the IR detection routine is to use a
PIN directive, and give
P11 a name like
irSenseLF, which stands for infrared-sensor-left-front. Likewise, you
can give P4 a name, like
IrLedLF, which is short for infrared-light-emitting-diode-left-
front. You can then use these pin names in place of the 4 and the
IN11. In addition, give
the value 38500 a constant name, like
IrFreq. Your code will then look like this:
IrLedLF PIN 4
IrSenseLF PIN 11
.
.
.
IrFreq CON 38500
.
.
.
irLF VAR Bit
.
.
.
FREQOUT IrLedLF, 1, IrFreq
irLF = IrSenseLF
The PIN directive has lots of advantages. For example, imagine you have a program that
uses the same I/O pin in 20 different places. What if you disconnect the circuit from the
I/O pin you were using and connect it to a different I/O pin? Instead of replacing 20
different references to the I/O pin, simply update the number in the
PIN directive, and
the program is fixed. Another
PIN directive advantage is that the BASIC Stamp Editor
looks at each command and decides whether you are using the I/O pin as an output or an
input.