Ultrasonic distance sensor with PIC16F84A


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    23

    Default Ultrasonic distance sensor with PIC16F84A

    All

    I used the code below to light a LED if the distance is less than 12 inches, the code runs , but for only 3 iterations, some time 4 iterations, I cannot seem to find out why, can anyone help.

    I am using the parallax ping sonar sensor

    Thanks



    -----------------------------------
    code
    ------------------------------------
    SYMBOL Ping = 7


    ' -----[ Constants ]-------------------------------------------------------

    SYMBOL Trigger = 1 ' 10 uS trigger pulse
    SYMBOL Scale = 10 ' raw x 10.00 = uS

    SYMBOL RawToIn = 889 ' 1 / 73.746 (with **)
    SYMBOL RawToCm = 2257 ' 1 / 29.034 (with **)

    SYMBOL IsHigh = 1 ' for PULSOUT
    SYMBOL IsLow = 0


    ' -----[ Variables ]-------------------------------------------------------

    SYMBOL rawDist = W1 ' raw measurement
    SYMBOL inches = W2
    SYMBOL cm = W3



    ' -----[ Initialization ]--------------------------------------------------

    Reset:


    ' -----[ Program Code ]----------------------------------------------------

    Main:

    GOSUB Get_Sonar ' get sensor value



    inches = rawDist ** RawToIn ' convert to inches
    'cm = rawDist ** RawToCm ' convert to centimeters

    if inches < 12 then pushd




    'DEBUG CLS ' report
    'DEBUG "Time (uS)..... ", #rawDist, CR
    'DEBUG "Inches........ ", #inches, CR
    'DEBUG "Centimeters... ", #cm

    PAUSE 500
    GOTO Main

    end




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

    ' This subroutine triggers the Ping sonar sensor and measures
    ' the echo pulse. The raw value from the sensor is converted to
    ' microseconds based on the Stamp module in use. This value is
    ' divided by two to remove the return trip -- the result value is
    ' the distance from the sensor to the target in microseconds.

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


    pushd:
    high 1 'high RB 1
    pause 1000
    LOW 1

    PAUSE 500

    HIGH 1
    PAUSE 1000
    LOW 1

    PAUSE 500
    return
    Last edited by MrRoboto; - 28th June 2009 at 02:15.

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    One problem is that your have a GOTO PUSHD...

    if inches < 12 then pushd

    ... but your have a RETURN at the end of the PUSHD code.

    Change your code to read...

    if inches < 12 then GOSUB pushd

  3. #3
    Join Date
    Dec 2008
    Posts
    23


    Did you find this post helpful? Yes | No

    Default Loop terminates on its own

    Thank you, but thats not the problem, there must be something inherinant to the ultrasonic sensor that wont let it infinite loop without being reset or something,

    Can anyone tell me why after four object detects it goes off based on the code I provided?

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    If you have added the GOSUB, then your code is now good.

    You could always check if RawDist=0 then light an Error LED, that way you can check if you are actually getting sensible results back from your Sensor.

Similar Threads

  1. Questions on ultrasonic distance finder
    By lilimike in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 20th April 2010, 22:19
  2. Distance Measurement Chips
    By sayzer in forum Off Topic
    Replies: 6
    Last Post: - 2nd February 2007, 21:57
  3. Serial Communication Using PIC16F84A
    By fazan83 in forum GSM
    Replies: 9
    Last Post: - 22nd January 2007, 03:56
  4. ultrasonic sensor
    By PoTeToJB in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 9th May 2006, 21:26
  5. GPS waypoint distance in Km
    By Art in forum Code Examples
    Replies: 1
    Last Post: - 28th August 2005, 02:59

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts