Shop OBEX P1 Docs P2 Docs Learn Events
ultra sonic distance sensor — Parallax Forums

ultra sonic distance sensor

Craig PaulCraig Paul Posts: 7
edited 2006-10-16 16:09 in BASIC Stamp
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?

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-10-16 12:27
    Craig -

    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 -->
  • Steve JoblinSteve Joblin Posts: 784
    edited 2006-10-16 13:33
    I think you would want your code to do the following:

    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
  • Vern GranerVern Graner Posts: 337
    edited 2006-10-16 16:09
    Craig Paul said...
    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.

    As an option, you might want to consider averageing a sequence of pings. I used this code in one of my projects:

    
    cntI var NIB
    sample var WORD
    AvgDistance con BYTE
    RawSample var WORD
    PING con 0 'pin for the PING sensor
    
    ' **************************
    ' * PING sonar             *
    ' **************************
      PINGSend:
    
      SAMPLE = 0                    ' reset the average ping time accumulator
    
      FOR cntI = 1 TO 10            ' sample the ping sensor "X" times
        PULSOUT PING, 5             ' Send a ping to check hand position
        PULSIN  PING, 1, RawSample  ' read the return time and store it in "RawSample"
        RawSample= RawSample MIN 100' make sure we don't drop below 0
        RawSample = RawSample/100   ' divide the raw sample by 100 to make it byte-sized :)
        Sample = Sample + RawSample ' Accumulate all the reduced PING samples
      NEXT
    
      AverageDistance=Sample/10 ' get the average distance
    
      RETURN
    
    



    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.

    smile.gif

    Vern

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Vern Graner CNE/CNA/SSE    | "If the network is down, then you're
    Senior Systems Engineer    | obviously incompetent so why are we
    Texas Information Services | paying you? Of course,if the network
    http://www.txis.com        | is up, then we obviously don't need
    Austin Office 512 328-8947 | you, so why are we paying you?" ©VLG
    
    
Sign In or Register to comment.