PING Sensor
Cchevs444
Posts: 2
I just recently got a PING sensor, and I've had some trouble with programing. I know all of my wiring is set up correctly because I am getting a successful response in the DEBUG window, and I have a program that works for converting the response time from us to inches. I want to turn this into a film canister finding program, where the bot can see the canister and go up to it. However, the moment I add "if..endif" statements, the program stops working. The bot thinks that the canister is right in front of it, and the bot never thinks that the canister is a further distance away than zero. Here is the code that I have been using, hopefully one of you can help me figure out what I am doing wrong, thanks.
' {$STAMP BS2}
' {$PBASIC 2.5}
Sonar PIN 11
'
[Constants]
CmConstant CON 2260 ' Conversion constants for room temperature measurements.
InConstant CON 890
InFar CON 30 ' Sets max distance canister can be so it does not see
InClose CON 5 ' anything outside that isnt the canister. InClose
' Is so that the Bot knows when the canister is right
' in front of it.
'
[Variables]
inDistance VAR Word
time VAR Word
counter VAR Word
'
[Main Program]
DO
PULSOUT Sonar, 5 'Sends out and recieves sonar signal
PULSIN Sonar, 1, time
InDistance = InConstant ** time 'Converts time to inches
DEBUG CR, DEC3 inDistance, " in" 'debugs the distance
IF InDistance < InFar AND InDistance > InClose THEN
GOSUB GoToCanister
ELSEIF InDistance < Inclose THEN
GOSUB Found
ELSE
GOSUB search
ENDIF
LOOP
'
[Subroutines]
'
search: 'bot spins slowely in search of the canister
counter = 1
FOR counter = 1 TO 3
PULSOUT 12, 735
PULSOUT 13, 735
PAUSE 20
NEXT
RETURN
'
GoToCanister:
'Bot moves forward to canister
counter = 1
FOR counter = 1 TO 3
PULSOUT 12, 850
PULSOUT 13, 650
PAUSE 20
NEXT
RETURN
Found: 'when bot has found the canister, it plays a sound
counter = 1
FREQOUT 4, 1000, 5000 'piezoelectric speaker is in pin 4
END
RETURN
'
' {$STAMP BS2}
' {$PBASIC 2.5}
Sonar PIN 11
'
[Constants]
CmConstant CON 2260 ' Conversion constants for room temperature measurements.
InConstant CON 890
InFar CON 30 ' Sets max distance canister can be so it does not see
InClose CON 5 ' anything outside that isnt the canister. InClose
' Is so that the Bot knows when the canister is right
' in front of it.
'
[Variables]
inDistance VAR Word
time VAR Word
counter VAR Word
'
[Main Program]
DO
PULSOUT Sonar, 5 'Sends out and recieves sonar signal
PULSIN Sonar, 1, time
InDistance = InConstant ** time 'Converts time to inches
DEBUG CR, DEC3 inDistance, " in" 'debugs the distance
IF InDistance < InFar AND InDistance > InClose THEN
GOSUB GoToCanister
ELSEIF InDistance < Inclose THEN
GOSUB Found
ELSE
GOSUB search
ENDIF
LOOP
'
[Subroutines]
'
search: 'bot spins slowely in search of the canister
counter = 1
FOR counter = 1 TO 3
PULSOUT 12, 735
PULSOUT 13, 735
PAUSE 20
NEXT
RETURN
'
GoToCanister:
'Bot moves forward to canister
counter = 1
FOR counter = 1 TO 3
PULSOUT 12, 850
PULSOUT 13, 650
PAUSE 20
NEXT
RETURN
Found: 'when bot has found the canister, it plays a sound
counter = 1
FREQOUT 4, 1000, 5000 'piezoelectric speaker is in pin 4
END
RETURN
'
Comments
If your program ever calls Found, it will stop since you have the END before the RETURN.
Why do you have "counter = 1" in Found and GoToCanister?
How about posting the version of your program that works and the "real" version of the one that doesn't work. You probably cut and pasted your source file and, although that works better for the Stamps than the Propeller where indenting is crucial, you do lose indenting and some punctuation. Use the "Go Advanced" button when posting a reply and attach your source files to the message (you'll see the instructions in the Advanced window).