ultra sonic distance sensor
Craig Paul
Posts: 7
I have been playing with the Ultra sonic sensor to try and detect a moving object.
I want to be able to tell whether it is coming closer, moving away or staying still.
using the code:
LOW Ping
PULSOUT 6, 5
PULSIN 6, 1, distance
distance = distance ** 2251
running this code twice and then comparing the value of distance. The problem is I need to round off the value of the VAR distance. Otherwise I get a slightly different answer even when things are not changing.
The DEC4 command is just for debug and does not round off the value.
Any suggestions?
I want to be able to tell whether it is coming closer, moving away or staying still.
using the code:
LOW Ping
PULSOUT 6, 5
PULSIN 6, 1, distance
distance = distance ** 2251
running this code twice and then comparing the value of distance. The problem is I need to round off the value of the VAR distance. Otherwise I get a slightly different answer even when things are not changing.
The DEC4 command is just for debug and does not round off the value.
Any suggestions?
Comments
If you will provide us with the entire program, or more of it than you've provided, I'm sure we can offer some suggestions.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
x· con [noparse][[/noparse]make this the amount of the sensitivity you want]
read the distance
store the distance value in a variable called "prior_distance"
check_sensor
·········· Read the distance
·········· store the distance value in a variable called "current_distance"
···········difference = current_distance - prior_distance
·········· prior_distance = current_distance
·········· if difference is > x then go to big_difference
·········· goto check_sensor
big_difference:
········· do what you want
As an option, you might want to consider averageing a sequence of pings. I used this code in one of my projects:
I didn't need the full resolution of the PING, so i reduced the ping result by 100, making it fit into a BYTE variable insterad of a word variable. I then accumulated 10 samples and took the average of those 10 to get a stable (non-jittery) distance measurement. Hope this helps.
Vern
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔