第一個程式:
' {$STAMP BS2}
' {$PBASIC 2.5}
DIRA = 0
DIRB = 15
DIRC = 15
DIRD = 15
OUTC = 0
c CON 300*10
d CON 500*10
e CON 600*10
KEYIN VAR Byte
DEBUG "Input a number (1 to 4)",CR
MAIN:
keyin=0
SERIN 16,16468,100,NoDATA,[KEYIN]
IF KEYIN="1" THEN FORWARD
IF KEYIN="2" THEN BACKWARD
GOTO MAIN
FORWARD:
OUTC=%1111
PAUSE 20
OUTC=0
PAUSE 50
RETURN
BACKWARD:
OUTC=%0101
PAUSE 20
OUTC=0
PAUSE 70
RETURN
第2個程式:
'{$STAMP BS2}
'{$PBASIC 2.5}
c CON 300*10
d CON 500*10
e CON 600*10
DIRA = %0000
DIRB = %1111
DIRC = %1111
DIRD = %1111
OUTC = 0
Ping PIN 12
cm VAR Word
rawDist VAR Word ' raw measurement
Trigger CON 5 ' for BS2 trigger pulse = 10 uS
Scale CON $200 ' raw x 2.00 = uS
RawToCm CON 2257 ' 1 / 29.034 (with **)
DO
GOSUB Get_Sonar ' get sensor value
cm = rawDist ** RawToCm ' convert to centimeters
DEBUG HOME,CLS,"Measure Distance is ",DEC cm ," cm",CR
PAUSE 300
IF cm <15 THEN
FREQOUT 15,150,c,c*2
FREQOUT 15,150,d,d*2
FREQOUT 15,150,e,e*2
ELSEIF cm >15 THEN
OUT15 = 0
ENDIF
LOOP
Get_Sonar:
Ping = 0 ' make trigger 0-1-0
PULSOUT Ping, Trigger ' activate sensor
PULSIN Ping, 1, rawDist ' measure echo pulse
rawDist = rawDist */ Scale ' convert to uS
rawDist = rawDist / 2 ' remove return trip
RETURN |