Page 204 ·
Robotics with the Boe-Bot
Boe-Bot should move forward. If you cast a shadow over one of the photoresistors, the
Boe-Bot should turn in the direction of the photoresistor that senses the shadow.
√
Enter, save, and run ShadowGuidedBoeBot.bs2.
√
Use your hand to cast shadows over the photoresistor dividers.
√
Study this program carefully and make sure you understand how it works. It is
very short, yet very powerful.
' Robotics with the Boe-Bot - ShadowGuidedBoeBot.bs2
' Boe-Bot detects shadows cast by your hand and tries to follow them.
' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
DEBUG "Program Running!"
FREQOUT 4, 2000, 3000 ' Start/restart signal.
DO
IF (IN6 = 0) AND (IN3 = 0) THEN ' Both detect shadows, forward.
PULSOUT 13, 850
PULSOUT 12, 650
ELSEIF (IN6 = 0) THEN ' Left detects shadow,
PULSOUT 13, 750 ' pivot left.
PULSOUT 12, 650
ELSEIF (IN3 = 0) THEN ' Right detects shadow,
PULSOUT 13, 850 ' pivot right.
PULSOUT 12, 750
ELSE
PULSOUT 13, 750 ' No shadow, sit still
PULSOUT 12, 750
ENDIF
PAUSE 20 ' Pause between pulses.
LOOP
How ShadowGuidedBoeBot.bs2 Works
The
IF…THEN
statement in the
DO…LOOP
looks for one of the four possible shadow
conditions: both, left, right, neither. Depending on which condition is detected,
PULSOUT
commands deliver pulses for one of the following maneuvers: forward, pivot right, pivot
left, or sit still. Regardless of the condition, one of the four sets of pulses will be
delivered each time through the
DO…LOOP
. After the
IF…THEN
statement, it’s important to
remember to include the
PAUSE 20
to ensure the low time between each pair of servo
pulses.