Shop OBEX P1 Docs P2 Docs Learn Events
srf04 — Parallax Forums

srf04

ArchiverArchiver Posts: 46,084
edited 2003-11-11 01:37 in General Discussion
Hi to all……..
I'm having a problem with my srf04 ultrasonic sensor
I'm using basic stamp 2 SX (BSX2) and I found this example on a
website
The problem is the readings that I get are not correct for example if
the distance is 20cm.
The readings that I get from the srf04 are:
21 inc. and 51 cm ……… (Please what is the problem?)
'
`Devantech SRF04 Example

wDist var word

INIT con 0
ECHO con 1


' CONVERSION FACTORS:
'
' The PULSIN command returns the round-trip echo time in 2us units
' which is equivalent to the one-way trip time in 1us units.
'
' distance = (echo time) / (conversion factor)
'
' use 74 for inches (73.746us per 1 in)
' use 29 for centimeters (29.033us per 1 cm)
'
convfac con 74 ' use inches
'
main

gosub sr_sonar
debug dec wDist, cr
pause 200
goto main


sr_sonar:

pulsout INIT,5 ' 10us init pulse
pulsin ECHO,1,wDist ' measure echo time
wDist=wDist/convfac ' convert to inches
pause 10
return
Sign In or Register to comment.