' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
' -----[ Variables ]----------------------------------------------------------
pulseCount VAR Byte ' FOR...NEXT loop counter.
counter VAR Nib ' Counts alternate contacts.
old7 VAR Bit ' Stores previous IN7.
old5 VAR Bit ' Stores previous IN5.
' -----[ Initialization ]-----------------------------------------------------
FREQOUT 4, 2000, 3000 ' Signal program start/reset.
counter = 1 ' Start alternate corner count.
old7 = 0 ' Make up old values.
old5 = 1
' -----[ Main Routine ]----------------------------------------
DO
IF (IN5 = 0) AND (IN7 = 0) THEN ' Both whiskers detect
FREQOUT 4, 100, 4000 ' 4 kHz beep for 100 ms
FREQOUT 4, 100, 4000 ' Repeat twice
GOSUB Back_Up ' Back up & U-turn
GOSUB Turn_Left
GOSUB Turn_Left
ELSEIF (IN5 = 0) THEN ' Left whisker contacts
FREQOUT 4, 100, 4000 ' 4 kHz beep for 100 ms
GOSUB Back_Up ' Back up & turn right
GOSUB Turn_Right
ELSEIF (IN7 = 0) THEN ' Right whisker contacts
FREQOUT 4, 100, 4000 ' 4 kHz beep for 100 ms
GOSUB Back_Up ' Back up & turn left
GOSUB Turn_Left
ELSE ' Both whiskers 1, no
GOSUB Forward_Pulse ' contacts
ENDIF ' Apply a forward pulse
LOOP
' -----[ Subroutines ]--------------------------------------------------------
Forward_Pulse: ' Send a single forward pulse.
PULSOUT 13,850
PULSOUT 12,650
PAUSE 20
RETURN
Turn_Left: ' Left turn, about 90-degrees.
FOR pulseCount = 0 TO 35
PULSOUT 13, 650
PULSOUT 12, 650
PAUSE 20
NEXT
RETURN
Turn_Right:
FOR pulseCount = 0 TO 35 ' Right turn, about 90-degrees.
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 20
NEXT
RETURN
Back_Up: ' Back up.
FOR pulseCount = 0 TO 40
PULSOUT 13, 650
PULSOUT 12, 850
PAUSE 20
NEXT
RETURN
Turn_Away:
GOSUB Back_Up
GOSUB Turn_Left
GOSUB Turn_Left
RETURN
To learn more HTML/CSS, check out these tutorials!