Shop OBEX P1 Docs P2 Docs Learn Events
PING Sensor — Parallax Forums

PING Sensor

Cchevs444Cchevs444 Posts: 2
edited 2011-05-25 06:57 in Accessories
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
'

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-24 12:51
    What do you mean by "adding if and endif statements"? Do you mean the ones in the main program above? What about the case where InDistance = InClose?
  • Cchevs444Cchevs444 Posts: 2
    edited 2011-05-25 00:25
    Yes i mean the ones added above. And I havent really started worrying about those little details yet because I havent gotten it to work at all, the program for some reason reads the PING sensor as 'zero' the moment I add the conditional statements that are in the code above.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-25 06:57
    You might want to put in a PAUSE after the DEBUG statement, something like PAUSE 50.

    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).
Sign In or Register to comment.