PDA

View Full Version : NEED help in writing some code for a ping sensor that use the POLLIN comand



sam_sam_sam
06-17-2005, 06:34 AM
"······http://forums.parallax.com/images/smilies/hop.gif·····THESE GUYS HAVE GONE MAD "····················http://forums.parallax.com/images/smilies/hop.gif···········
···················"·THEY NEED HELP BAD"
·····http://forums.parallax.com/images/smilies/hop.gif·I would like to use 4 ping sensors on bs2p.
····http://forums.parallax.com/images/smilies/jumpin.gif I would like do same thing with this program dose.
But·I want·to·use 4·ping sensors and have the output come·ON·by any one of the sensors
http://forums.parallax.com/images/smilies/idea.gif·How would·I use the POLLIN command or can·I· use the POLLIN command

CAN ANY ONE HELP ME·with this· http://forums.parallax.com/images/smilies/idea.gif
I have not used a·bs2p before.
I have used a bs2.
I am reading the reference manual 2.1

pollin
pollmode
pollout
pollrun
pollwait
····
http://forums.parallax.com/images/smilies/cool.gif· Thanks for any help that you give me··http://forums.parallax.com/images/smilies/cool.gif·.

······················ samsam

Post Edited (sam_sam_sam) : 6/16/2005 11:38:47 PM GMT

Jon Williams
06-17-2005, 07:15 AM
Using pin polling on the BS2p will not help with the Ping))). After it's triggered you need to measure the input pulse. With pin polling, you can detect a change of state and do something, but measurement of a pulse width is not possible. Use PUSLOUT to trigge the Ping))) and PULSIN to measure the return.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

sam_sam_sam
06-17-2005, 10:31 AM
·················································· ·················································· ····http://forums.parallax.com/images/smilies/hop.gif··········································· ······································· http://forums.parallax.com/images/smilies/jumpin.gif

············ ·THESE··THESE GUY HAVE GONE MAD················

························· THEY NEED HELP

·Jon·(or any one else)·will this work the way i have it here

I am going to buy a bs2p very soon i do not have one yet··········· http://forums.parallax.com/images/smilies/idea.gif

I thank·you for your help in this matter

samsam

' {$STAMP BS2p}
' {$PBASIC 2.5}


#SELECT $STAMP
#CASE BS2SX, BS2P
Trigger···············CON············· 13
Scale················ CON··········· $0CD················ ' raw x 0.80 = uS

#ENDSELECT

RawToIn············· CON·········· $0379·············· ' 1 / 73.746


' -----[ Variables ]-------------------------------------------------------
------------------------------------------------------------------------
time_1···· VAR····· ·Word··································· http://forums.parallax.com/images/smilies/idea.gif
time_2···· VAR····· ·Word···································'
time_3···· VAR····· ·Word
time_4··· ·VAR··· ·· Word········ 'I am not sure where to put this in code
ping········VAR····· ·Word·········' or if this right·
------------------------------------------------------------------------
rawDist···· ·VAR··· ·Word······························ ' raw measurement
inches······ ·VAR··· ·Word

Reset:
DEBUG CLS,·············································· · ' setup report screen
"Parallax Ping Sonar", CR,
"-------------------", CR,
CR,
"Time (uS)..... ", CR,
"Inches........ ", CR,

------------------------------------------------------------------------
pulsout 15, 5
pulsin 15, 1, time_1············ 'I am not sure where to this put in code
pasue 1000
······································ ·' or if this right
pulsout 14, 5
pulsin 14, 1, time_2
pasue 1000


pulsout 13, 5
pulsin 13, 1, time_3
pasue 1000


pulsout 12, 5
pulsin 12, 1, time_4
pasue 1000
------------------------------------------------------------------------

Main:
·············································· http://forums.parallax.com/images/smilies/idea.gif
DO
-------------------------------------------------------------------------
POLLIN time_1, 4
POLLIN time_2, 4
POLLIN time_3, 4·······················'I am not sure where to this put in code
POLLIN time_4, 4·······················' or if this right
POLLOUT ping, 3
-------------------------------------------------------------------------

GOSUB Get_Sonar··································· ' get sensor value
inches = rawDist ** RawToIn···················· ' convert to inches


DEBUG CRSRXY, 15, 3,·······························' update report screen
DEC rawDist, CLREOL
DEBUG CRSRXY, 15, 4,
DEC inches, CLREOL

'Light LED OUTPUT
IF inches > 10 THEN LOW 2
IF inches < 12 THEN HIGH 2

PAUSE 100
LOOP
END


END

' -----[ Subroutines ]-----------------------------------------------------

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
···································


Post Edited (sam_sam_sam) : 6/17/2005 4:35:01 AM GMT

Jon Williams
06-17-2005, 10:10 PM
Why do you think you need POLLIN?· If you simply want one subroutine that will work with multiple Ping))) sensors, do it like this (note that pingPin is a Nib variable, indicating which pin to measure distance with):

Get_Sonar:
· pingPin·= 0·······················' make trigger 0-1-0
· PULSOUT pingPin, Trigger··········' activate sensor
· PULSIN pingPin, 1, rawDist········' measure echo pulse
· rawDist = rawDist */ Scale········' convert to uS
· rawDist = rawDist / 2·············' remove return trip
· RETURN

From your code, you could loop through an array of sensors like this:

Main:
· FOR idx = 0 TO 3
··· pingPin = idx + 12··············'·Pings on P12..P15
··· GOSUB Get_Sonar
··· distance(idx) = rawDist
· NEXT

POLLIN will not serve you here; you can forget about it completely for this application.· And please ... no more jumping icons (those things give me a headache!).



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

sam_sam_sam
06-18-2005, 09:33 AM
Jon
I want to have an output be ON If one or more PING))) Sensor has an object in front of it
and be able to measure it that is why i was using pollin command but if what you said will
work ok but i want to use something like this.......
IF inches >10 THENLOW2
IF inches >12 THENLOW2
for any Ping))) sensor.
I also want to know which sensor is triggered and display it on a LCD DISPLAY.
DO I need more than one subroutine if any one sensor is triggered.
Is the code for each sensor
ping_1 pin 12
ping_2 pin 13
and so on
Do I need write same code for each sensor
Main:
FOR idx = 0 TO 3
"pingPin = idx + 12 " ' Pings on P12..P15 ????
"What dose this do i was looking for this in the
online help but i heve not found it yet" >
GOSUB Get_Sonar
distance(idx) = rawDist
NEXT


I want to THANK YOU for your help in this matter.
I will try not to give you a headache with this but
I am tring to learn how to write my own but it is hard to teach an old dog new trick

thanks samsam

Post Edited (sam_sam_sam) : 6/18/2005 2:41:10 AM GMT

Philip Gamblin
06-18-2005, 10:13 AM
I would suggest that you write your program incrementally. Learn how to get one sensor to measure. Then get it to measure and resopnd to your 10" and 12" limits. Get one sensor to behave how you want. Then add the display. Once you have the complete operation for one sensor, all you need is to create a loop that lets you operate multiple sensors. Remember you have to ping one sensor and get the echo measurement before you ping the next sensor etc.

Jon Williams
06-18-2005, 10:37 AM
I think Philip is right: you need to learn to walk before you run.

A couple things...

1) You don't want to trigger multiple sensors at the same time; echoes bouncing around from one sensor could foul the readings of another.· Read them one at a time as I showed you.

2) If your outputs are all in a row, then you can modify the main code like this to control them as you wish:

Main:
· FOR idx = 0 TO 3
··· pingPin = idx + 12··············'·Ping sensor attached to P12..P15
··· GOSUB Get_Sonar
··· distance(idx) = rawDist */ RawToIn
··· IF (distance(idx)·< 12) THEN
····· LOW (idx + 8)················ ' LEDs on P8..P11
··· ELSE
····· HIGH (idx + 8)
··· ENDIF
· NEXT

Now the code converts the distance to inches (and saves it in an array), then compares that to 12.· The way the loop works if the Ping connected to P12 measures a distance less than 12, it sets P8 low.· The control works like this:

Ping on P12 controls LED on P8
Ping on P13 controls LED on P9
Ping on P14 controls LED on P10
Ping on P15 controls LED on P11

3) You didn't say what kind of LCD you're using (that detail matters).· Again, learn how to use the LCD you have separately, then incorporate it into your Ping))) program.

4) Finally, you might consider working through our "What's A Microcontroller?" text -- it's a great foundation to BASIC Stamp programming.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

sam_sam_sam
07-13-2005, 09:46 AM
Jon(or any one else)·will this work the way·code is writen


' {$STAMP BS2}
' {$PBASIC 2.5}
ping······ PIN····· 12

#SELECT $stamp
· #CASE BS2
· trigger·· CON·· 5
· scale···· CON·· $200
#ENDSELECT
rawtoin··· CON··· 889
rawdist··· VAR··· Word
idx······· VAR··· Word
distance·· VAR··· Word
pingpin·· VAR··· Byte
Get_Sonar:
· pingPin = 0······································ ' make trigger 0-1-0
· PULSOUT pingPin, Trigger················· · ' activate sensor
· PULSIN pingPin, 1, rawDist················· ·' measure echo pulse
· rawDist = rawDist */ Scale················ ·' convert to uS
· rawDist = rawDist / 2······················ · ' remove return trip
· RETURN
Main:············································· ····' Ping sensor attached to P12
· FOR idx = 0 TO 3····························· ·' Ping sensor attached to P13
··· pingPin = idx + 12·························· ·' Ping sensor attached to P14
··· GOSUB Get_Sonar·························· ·' Ping sensor attached to P15
··· distance(idx) = rawDist */ RawToIn
··· IF (distance(idx) < 12) THEN
····· LOW (idx + 8)
··· ELSE····························· ·'Ping on P12 controls LED on P8
···· HIGH (idx + 8)················· 'Ping on P13 controls LED on P9
········································· 'Ping on P14 controls LED on P10
········································ ·'Ping on P15 controls LED on P11
··· ENDIF
· NEXT

I want to THANK YOU for your help in this matter.

samsam

sam_sam_sam
07-13-2005, 10:52 AM
I·just·got·the·LCD·Stock#: 27977· Parallax 2x16 Serial LCD backlit ·display·in·today··and·ping·sensors. ·I·will·be··doing·demo·that·came·with·the·Lcd·but· it·is·no·clear to me· how to turn on the··backlight·.
I want to do is turn on·the backlight· when there is something in
front of the ping sensor so·I can see·lcd display·it in the dark··but when there is·nothing there the backlight is off
·" Finally, you might consider working through our "What's A Microcontroller?" text -- it's a great foundation to BASIC Stamp programming."
I· have· done·the "What's A Microcontroller text·and it's a great foundation·.
How to write code for·more than one PING )))sensor·and to use·"·IDX·+12 "to write code for·more··than one sensor
it is not cover iin the text·I am glad that you are willing to help the one like me·who are having a hard time understanding thing like this I DO WANT TO THANK YOU FOR THIS·.I also like reading "NUT & VOLTS I ENJOY IT!!!!!
·················································· ·················································· ·················································· ·················································· ·················································· ·················································· ·················································· ······················You

THANK YOU FOR ALL YOUR HELP IN THIS MATTER



······· samsam
·

Bruce Bates
07-13-2005, 11:27 AM
Sam -

You are dropping through a RETURN statement that was never set by a GOSUB.
This occurs right in the beginning of your program:

Get_Sonar:
pingPin = 0 ' make trigger 0-1-0
PULSOUT pingPin, Trigger ' activate sensor
PULSIN pingPin, 1, rawDist ' measure echo pulse
rawDist = rawDist */ Scale ' convert to uS
rawDist = rawDist / 2 ' remove return trip
RETURN <<== ** ERROR **

Regards,

Bruce Bates

Chris Savage
07-14-2005, 03:50 AM
Sam,

·· Your subroutine needs to come after the Main routine, and the Main routine will need an END statement to keen the main routine from falling back through into it again after it's finished.



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

sam_sam_sam
07-17-2005, 01:10 AM
This code dose work very well http://forums.parallax.com/images/smilies/yeah.gif
' {$STAMP BS2}
' {$PBASIC 2.5}
ping······ PIN····· 12
ping_1···· PIN····· 13

#SELECT $stamp
· #CASE BS2
· trigger·· CON·· 5
· scale···· CON·· $200
#ENDSELECT
rawtoin···· CON··· 889
IsHIGH······ CON· 1
IsLow······· CON· 0
rawdist··· VAR··· Word
distance·· VAR··· Word
idx······· VAR··· Nib
pingpin··· VAR··· Word
· ' Ping ON P13 controls LED ON P15
Main:
· DO
· FOR idx = 0 TO 1······················ ' Ping sensor attached to P12
··· pingpin = idx + 12·················· ' Ping sensor attached to P13
··· GOSUB Get_Sonar
··· distance(idx) = rawDist */ RawToIn
····· DEBUG CRSRXY, 15, 3,
········· DEC rawDist, CLREOL,··········'
········· CRSRXY, 15, 4,
········· DEC· distance, CLREOL


··· IF (distance(idx) < 5000) THEN ledlight


··' Ping ON P12 controls LED ON P14
·' Ping ON P13 controls LED ON P15


··· NEXT

· IF (distance(idx) > 1000) THEN time
······ LOOP

· Get_Sonar:
· ping = 0······························ ' make trigger 0-1-0
· PULSOUT pingpin, Trigger·············· ' activate sensor
· PULSIN pingpin, 1, rawDist············ ' measure echo pulse
· rawDist = rawDist */ Scale············ ' convert to uS
· rawDist = rawDist / 2················· ' remove return trip
·RETURN
···· ledlight:
· HIGH·(idx + 14)
· GOTO time
···· ledoff:
· LOW·(idx + 14)
·GOTO main
·time:
PAUSE 2000
GOTO· ledoff

Can you tell me why this code will not work the way it writen http://forums.parallax.com/images/smilies/confused.gif

' {$STAMP BS2}
' {$PBASIC 2.5}
ping······ PIN····· 12
#SELECT $stamp
· #CASE BS2
· trigger·· CON·· 5
· scale···· CON·· $200
#ENDSELECT
rawtoin··· CON··· 889
IsHigh···· CON···· 1······················ ' for PULSOUT
IsLow····· CON···· 0
rawdist··· VAR··· Word
idx······· VAR····· nib
distance·· VAR··· Word
pingpin··· VAR··· word
·Main:········································ ' Ping sensor attached to P12
· FOR idx = 0 TO 1···························· ' Ping sensor attached to P13
··· pingPin = idx + 12
··· GOSUB Get_Sonar
··· distance(idx) = rawDist */ RawToIn
··· IF (distance(idx) < 5000) THEN
····· LOW (idx + 14)
··· ELSE
···· HIGH (idx + 14)
···································· 'Ping on P12 controls LED on P14
···································· 'Ping on P13 controls LED on P15
··· ENDIF
· NEXT
· Get_Sonar:
· pingPin = 0································ ' make trigger 0-1-0
· PULSOUT pingPin, Trigger··················· ' activate sensor
· PULSIN pingPin, 1, rawDist················· ' measure echo pulse
· rawDist = rawDist */ Scale················· ' convert to uS
· rawDist = rawDist / 2······················ ' remove return trip
· RETURN

I would like to THANK EVERYONE who has help me in this matter· THANK YOU ........
samsam

Post Edited (sam_sam_sam) : 7/17/2005 1:14:01 AM GMT

Chris Savage
07-17-2005, 12:42 PM
Probably the same reason as before...Your main routine drops into the subroutine after completion.· You have no END statement to prevent this.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

sam_sam_sam
07-20-2005, 10:10 AM
Chris (or any one else)
I would like to make an out put go on and off
using this
IF (distance(idx) < 5000) to turn on·
and use the··COUNT command·(" event-based program deay·") used in Stamp Works ver.1.2
pages 105 to 107 exeperiment #16 counting events
IF (distance(idx) > 1000) to turn·· off
Would this work and how would·I write it

Thank You for any help in this matter

samsam
·