Shop OBEX P1 Docs P2 Docs Learn Events
SRF04 Source Code — Parallax Forums

SRF04 Source Code

NhoybNhoyb Posts: 9
edited 2005-03-31 16:22 in Learn with BlocklyProp
Anyone please help, i need a source code for SRF04 that will continousely check distance not more than 2.5 meters, im just new to micro controllers and i need to apply this on my project using a bs2sx with boe. Any help will be appreciated.

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-03-11 14:18
    Nhoyb -

    Here is a link to a sample program for the Stamp BS-2 for the Devantech SRF04:
    http://www.acroname.com/robotics/info/examples/srf04-1/srf04-1.html
    Please also note the comments about the SRF04 power requirements on the above web page as well. This is a most important issue which is often overlooked.

    You will have to modify the duration parameter of the PULSOUT command to reflect the increased speed of the BS-2SX over the BS-2. The PULSOUT command requires the specification of some number of timer units (TU) to set the duration of the outgoing pulse. For the BS-2 the TU's are of 2.0 uS duration and on the BS-2SX the TU's are of .8 uS duration. There may be other modifications necessary as well, as I didn't inspect the program thoroughly.

    There is an additional application note (R93-SRF04p.PDF) about the SRF04, written by Parallax, which can be downloaded from here:
    http://www.acroname.com/robotics/parts/R93-SRF04.html

    Regards,

    Bruce Bates
  • Ken GraceyKen Gracey Posts: 7,392
    edited 2005-03-11 21:27
    Bruce,

    We didn't write the application on the Acroname site.

    But Nhoyb can get our documentation here:

    http://www.parallax.com/detail.asp?product_id=28015-SRF04

    Ken Gracey
    Parallax, Inc.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-03-11 22:15
    Ken -

    I beg to differ with you. If you download the file that I indicated you will see that the information was produced by Parallax. Perhaps it's being used without your permission, but it is a Parallax document. I actually tried to find such a document on the Parallax web site first but wasn't able to find it, thus the reference to the Acroname copy (R93-SRF04p.PDF).

    If you'd like me to send you a copy, I'd be happy to do so.

    Regards,

    Bruce Bates
  • NhoybNhoyb Posts: 9
    edited 2005-03-13 03:50
    Thanks to you both, your links helped a lot.
  • NhoybNhoyb Posts: 9
    edited 2005-03-13 14:14
    OK. Heres my new problem. This is the source code I used for SFR04.


    ' =========================================================================
    '
    ' File....... SRF04_Demo.BS2
    ' Purpose.... Devantech SRF04 Ultrasonic Range Finder
    ' Author..... Parallax, Inc. (Copyright 2003 - All Rights Reserved)
    ' E-mail..... support@parallax.com
    ' Started.... 06 MAR 2002
    ' Updated.... 01 OCT 2003
    '
    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}
    '
    ' =========================================================================


    '
    [noparse][[/noparse] Program Description ]
    '
    ' This program uses the Devantech SRF04 to measure the distance between the
    ' unit and a target. Display is raw value, centimeters, and inches.


    '
    [noparse][[/noparse] Revision History ]
    '
    ' 01 OCT 2003 : Updated for PBASIC 2.5 and for any BASIC Stamp module


    '
    [noparse][[/noparse] I/O Definitions ]
    '
    Trigger PIN 0
    Echo PIN 1


    '
    [noparse][[/noparse] Constants ]

    #SELECT $STAMP
    #CASE BS2, BS2E
    Trig10 CON 5 ' trigger pulse = 10 uS
    ToCm CON 30 ' conversion factor to cm
    #CASE BS2SX, BS2P
    Trig10 CON 13
    ToCm CON 78
    #CASE BS2PE
    Trig10 CON 5
    ToCm CON 31
    #ENDSELECT


    '
    [noparse][[/noparse] Variables ]

    samples VAR Nib ' loop counter
    pWidth VAR Word ' pulse width from sensor
    rawDist VAR Word ' filtered measurment
    cm VAR Word ' centimeters
    inches VAR Word


    '
    [noparse][[/noparse] EEPROM Data ]


    '
    [noparse][[/noparse] Initialization ]

    Setup:
    LOW Trigger
    DEBUG CLS,
    "Devantech SRF04 Demo", CR,
    "
    ", CR,
    "Raw........... ", CR,
    "Centimeters... ", CR,
    "Inches........ "


    '
    [noparse][[/noparse] Program Code ]

    Main:
    DO
    GOSUB Get_Sonar ' take sonar reading
    DEBUG CRSRXY, 15, 2, DEC rawDist, CLREOL

    cm = rawDist / ToCm ' convert to centimeters
    DEBUG CRSRXY, 15, 3, DEC cm, CLREOL

    inches = cm */ $03EF ' x 3.937 (to 0.1 inches)
    DEBUG CRSRXY, 15, 4,
    DEC inches / 10, ".", DEC1 inches,
    CLREOL

    PAUSE 250 ' delay between readings
    LOOP

    END


    '
    [noparse][[/noparse] Subroutines ]

    Get_Sonar:
    rawDist = 0 ' clear measurement
    FOR samples = 1 TO 5 ' take five samples
    PULSOUT Trigger, Trig10 ' 10 uS trigger pulse
    #SELECT $STamp
    #CASE BS2, BS2E
    RCTIME Echo, 1, pWidth ' measure pulse
    #CASE #ELSE
    PULSIN Echo, 1, pWidth ' measure pulse
    #ENDSELECT
    rawDist = rawDist + (pWidth / 5) ' simple digital filter
    PAUSE 10 ' minimum period between
    NEXT
    RETURN



    It works fine and its great, Im confuse on adding a new command line that will make my piezzo speaker and LED, that will light up and buzz when the taken measurement is; lets say under 100 inches. This project is suppose to be an alarm. And I'd like to trigger a pin for a led and piezzo speaker when a certain value is less than indicated. The "What is Microcontroller" doesnt teach me well in programming. Specially the CASE and IF THEN statement.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-13 15:58
    Okay, Nhoyb, I'm going help ... but I'm going to challenge you first.· Did you really work your way through WAM, or did you just skim?· WAM is an excellent training book and I believe anyone who works their way through it -- diligently -- will come out a good BASIC Stamp programmer

    Perhaps you're rushed, and will go back.· Until then, this is what you should have come up with:

    Check_Alarm:
    · IF (inches < 1000) THEN·········' distance less that 100.0 inches?····
    ··· HIGH AlarmLed·················' yes -- alarm on·
    ··· FREQOUT·Piezo, 1250, 180····· '········sound beeper
    · ELSE
    ··· LOW AlarmLed················· ' no -- clear alarm
    · ENDIF

    I will leave it to you to create the pin definitions for the·piezo and alarm LED, and then·insert the code into the proper place in the program.· Since this is the Stamps In Class forum I'm assuming that this is a class project ... so I won't give you all the answers (maybe so other soul will, but that would rob you of the learning experience....).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA


    Post Edited (Jon Williams) : 3/13/2005 4:01:07 PM GMT
  • NhoybNhoyb Posts: 9
    edited 2005-03-28 14:01
    Jon,



    Thanks, It took me 3 weeks to review the What's a Microcontroller and figured where to insert the code you've given. It works great. But I still have some questions, like; how can BS2 measure distance from SRF04? I still dont understand it, where does the RAW value comes from? And from how to convert RAW from Inches. This is very very confusing. The manual sez that the ultra sonic sound travels at 1.12feet per ms, but I dont see that value on the source code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Soul Still Bruns!!
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-28 15:07
    Here are the essentials (you can find this in the docs):

    The BS2 triggers the SRF-04 to send a sonic burst. The SRF-04 will create an echo pulse that has a width that corresponds to the distance to the target. The BS2 actually measures the width of the echo pulse and derives the distance from it. Remember that this distance measurement, initially, is in units of time (microseconds). Since sound moves at about one inch in 74 micoseconds, if we divide the time value by 74 we can get inches.

    Acroname sells the SRF-04 (we don't carry them anymore) and has lots of info on the SRF-04.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • NhoybNhoyb Posts: 9
    edited 2005-03-31 10:36
    Jon,



    I have inserted this code on my project.

    IF (inches < 300) THEN
    HIGH 14
    FREQOUT 9, 1250, 1800

    ELSE
    LOW 14·····································
    LOW 12
    LOW 11

    ENDIF
    IF (inches < 300) THEN
    LOW 14


    ENDIF

    IF (inches < 200) THEN


    LOW 12
    HIGH 11

    ENDIF

    IF (inches < 100) THEN


    HIGH 12
    LOW 11
    FREQOUT 3, 1250, 1800


    ENDIF

    NEXT
    RETURN

    It works but there is a delay and I think its also conflicting with the Pin 14 which is a led, i cant make it shut off, when the other endif statement meet its condition. I'v also tried to use the goto and gosub command but could not make it work. Also I used 2 piezo speaker, which not seemed logical, I just want the piezo speaker to beep faster when object is getting nearer with a reasonable delay. Please Jon Williams, Im just new to programming. Im trying my best here to learn, but I still need help.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Soul Still Bruns!!
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-31 16:22
    What you can do is use the distance to calculate a delay value to insert between the beeps -- that way as the object gets closer the beeping will get faster. To calculate the delay, you probably just need to divide the distance by some value (you need to experiment with the value to suit your tastes).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
Sign In or Register to comment.