Ultrasonic distance sensor with PIC16F84A


Results 1 to 4 of 4

Threaded View

  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 01:15.

Similar Threads

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