I’ve finally started working on an upgrade for my rover and I’m using a couple ping sensors on it. I’m going to need them to make more precise measurements than inches.(milimeters or centimeters) So what I need is the needed math to convert a ping distance input into a metric conversion. This is the code I’m using.
[code]wdist var word
main:
low p4
pulsout p4, 5
input p4
pulsin p4, 0, toolong, 40000, wdist
wdist = wdist / 148 ;convert for inches
serout s_out, i9600, "Distance: ", sdec wdist, 13, 10] ;display result in terminal
goto main
toolong: ; if the program gets here the sensor is not functioning, or wired wrong
serout s_out, i9600, “Timeout, sensor is not working”, 13]
goto main
[/code]
I’m guessing that to convert the input to centimeters or milimeters will take a decimal number. So I"ll have to do some conversions to floating point, right?
You could go to floating point, but I usually always avoid this…
There is code in the documentation PDF (parallax.com/Portals/0/Downl … G-v1.6.pdf) up on the parallax website for the PING sensor, that shows code for several different platforms. You might try the one for the BS2 and see if that works. Or you might try to translate the code for the propeller…
It might look something like:
TO_IN con (73746*2) ' Inches - *2 as I think pulsin returns .5us
TO_CM con (29034*2) ' Centimeters
D_Inches var word
D_CM var word
...
;do your ping stuff
d_Inches = (wdist * 1000)/TO_IN
d_CM = (wdist * 1000)/TO_CM
Note: d_Inches comes out pretty close to what you had, except that it might have a little more accuracy…
if D_cm > 20 then
sound 9, [50\500]; this noise for measurements beyond 20 centimeters
else
sound 9, [50\100]; this one for inside 20 centimeters
endif
serout s_out, i9600, "Distance: ", sdec wdist, 13, 10] ;display result in terminal
goto main
toolong: ; if the program gets here the sensor is not functioning, or wired wrong
serout s_out, i9600, “Timeout, sensor is not working”, 13]
goto main[/code]