Page 222 ·
Robotics with the Boe-Bot
average = tim timeLeft / 2
difference = average / 6
Now, the
difference
variable can be used in this
IF…THEN
statement, and it will be a
large value when the lighting is low, and a small value when the lighting is bright.
IF (timeLeft > tim difference) THEN ' Turn right.
PULSOUT 13, 850
PULSOUT 12, 850
ELSEIF (timeRight > ti difference) THEN ' Turn left.
PULSOUT 13, 650
PULSOUT 12, 650
ELSE ' Go forward.
PULSOUT 13, 850
PULSOUT 12, 650
ENDIF
Example Program – RoamingTowardTheLight.bs2
Unlike RoamingWithPhotoresistorDividers.bs2 on page 201, this program will be very
responsive to your hand casting a shadow over the photoresistor, regardless of whether
the light is bright or dim. This program does not need to change resistors depending on
the lighting conditions. Instead, it takes into account the lighting conditions and the
sensitivity adjustment is made in software using the
average
and
difference
variables.
For this program to work well, your photoresistors should respond similarly to similar
light levels.
If the RC circuits are severely mismatched, your measurements from Table 6-1
will be very different under the same lighting conditions. You can correct these mismatched
measurements using techniques discussed in Appendix F: Balancing Photoresistors.
This program measures the overall average of
timeLeft
and
timeRight
and uses it to
set the
difference
between the
timeLeft
and
timeRight
measurements that’s needed
to justify delivering a turning pulse.
√
Enter, save, and run RoamingTowardTheLight.bs2
√
Take it to various areas, and let it roam, and verify that you can change its course
by casting a shadow over one of the photoresistor RC circuits, regardless of the
lighting conditions.