Shop OBEX P1 Docs P2 Docs Learn Events
Beeping Sonar — Parallax Forums

Beeping Sonar

CrystalionxCrystalionx Posts: 18
edited 2005-10-07 18:55 in BASIC Stamp
I have set up the sonar ranging unit, and now I want it to emit a beep at particular distances, through a piezo or other analog unit.· I am going to try to cut and paste one of the audio programs with if.then statement linked to certain distances.· I have no idea if this will work.· Is there an already existing program to accomplish this - if not, any suggestions?

Post Edited By Moderator (Jon Williams (Parallax)) : 9/28/2005 5:17:22 PM GMT

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-28 17:25
    You'll probably have to write your own program -- but it's not hard and there are several programming constructs that can be used (IF-THEN-ENDIF, SELECT-CASE, LOOKDOWN and BRANCH, ON-GOSUB ...). You give no specifics so you'll only get generalized responses.

    And do be sure to add an appropriate subject heading to your new threads (I fixed this one).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-09-28 18:29
    Crystalionx, please refrain from posting duplicate questions, you asked this same question just yesterday in this thread http://forums.parallax.com/showthread.php?p=551400

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • CrystalionxCrystalionx Posts: 18
    edited 2005-09-28 20:18
    Sorry, about the double-posting - I thought that I had done the procedure correctly the first time.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-09-29 20:09
    Its ok, it just confusing to those trying to answer you questions, and more likely you'll miss people answering your question. For instance did you see my code example in the other thread?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • CrystalionxCrystalionx Posts: 18
    edited 2005-09-29 23:04
    Paul - no I did not see your code segment - and I definitely would like to. I am going to search for it now.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-09-29 23:11
    click on this link: http://forums.parallax.com/showthread.php?p=551400

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • CrystalionxCrystalionx Posts: 18
    edited 2005-10-04 17:13
    So far I've not succeeded in setting up a program for correlating sonar distance ranging with an audio signal, employing either Lookdown or END/IF, though I've tried all kinds of combinations, including pasting in the code segment which Paul sent me and altering the distance values - even using the exact values for the indexes has not worked.··I am attempting to get the piezo to beep at 350cm, 175cm, 85 cm, and 40cm.· I added a pin out command from the standard piezo program but that did not work either.· Perhaps it's just a matter of the exact setting up of the command sentence structure.· I will keep trying, but I certainly would appreciate further suggestions.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-10-04 17:53
    Crystalionx said...
    I am attempting to get the piezo to beep at 350cm, 175cm, 85 cm, and 40cm.·
    Does this mean you want the peizo to beep when an object is at these distances, or are they the boundries between the different zones?

    From your posts, I gather your sonar code is working, what are the values associated with 350, 175, 85 and 40 cm?

    Have you tested the functioning of the piezo seperately? IE hooking it up and sending it various FREQOUT commands (straight FREQOUT commands, none of the sonar code or conditional statements)? If so does it work as expected?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 10/4/2005 5:56:47 PM GMT
  • CrystalionxCrystalionx Posts: 18
    edited 2005-10-04 19:53
    The only values associated with the particular distances are the time responses for the echo return that are indicated on the deburg screen - I will need to get back into our school lab on Thuirsday to obtain this data.· The basic idea is that at four different specific distances, the piezo - or any other audio unit connected to the microprocessor - will beep in order to depict that distance from a solid object.· The ideal situation is a high frequency beep as the sonic ranger approaches the object.· The piezo itself works - I have it mounded on the B2 board - but only by means of a separate program (the standard one from the book).· As I said, I am trying out variious techniques such as if/then - perhaps it's just a matter of moving commandsaround.· By the way, radio shack audio components work with the B2 - you just connect the leads from say, a mini-speaker into the connecting points utilized by the piezo.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-10-04 21:15
    I apologise, but Im still confused, if an object is 65 cm away, do you want·the piezo to·beep? Or·only to beep if it is at one of the 4 distances you specified?

    Next time you are at the lab, can you place a debug statement in to see what values are being returned by the Lookdown·and Lookup command? Also posting the portion of code relating to this would be helpful (maybe a syntax error is present, or maybe I wrote the code down wrong, I can do a testing of the code section in question, though it may take me a little bit to get it setup)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 10/4/2005 9:20:10 PM GMT
  • CrystalionxCrystalionx Posts: 18
    edited 2005-10-04 22:04
    I want the piezo to beep at four completely separate distances - say, 350cm, then 170cm, then 85, then 40 - our separate beeps, each one at a higher frequency as you get closer to avoid confusion.

    This is the code I'm using:


    'PingCentimeters.BS2.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    time VAR Word
    DO
    PULSOUT 15, 5
    PULSIN 15, 1, time
    DEBUG HOME, "time = ", DEC5 time
    time = time ** 2251
    DEBUG CR, "Distance = ", DEC4 time, " cm"
    PAUSE 100
    LOOP

    Range VAR Word
    Freq VAR Word

    'Get the sonar value and place in Range

    LOOKDOWN Range, <[noparse][[/noparse]64, 128, 192, 255], Freq
    LOOKUP Freq, [noparse][[/noparse]10000, 5000, 1000, 500], Freq
    FREQOUT Piezo, 1000, Freq




    When I ruin this I get an error message asking for values for freq. I can send you the error messages later.

    I believe I grasp the index concept and I have used different values - I never get past RUN in any case.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-10-04 23:24
    Crystalionx·-

    That can't be the program, or at least it's not the whole program. The program you provided never asks for any input, and it also goes into an infinite loop, unless I'm missing something. The DO ... LOOP with no exit strategy is causing that latter problem.

    Lastly, the following code is never being executed, if that is indeed a complete program:

    LOOKDOWN Range, <[noparse][[/noparse]64, 128, 192, 255], Freq
    LOOKUP Freq, [noparse][[/noparse]10000, 5000, 1000, 500], Freq
    FREQOUT Piezo, 1000, Freq

    Are you sure these programs you're writing are actually getting downloaded to the Stamp? Is there more to this program that you haven't provided?

    Regards,

    Bruce Bates



    Post Edited (Bruce Bates) : 10/4/2005 11:36:58 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-10-05 14:20
    Bruce is correct that the code I gave you is never executed since it is outside your program loop, Ill take a closer look at lunch time to see if there are any other issues. Also the code I gave you won't work as you want it to, because at the time I thought you wanted 4 zones rather than 4 distinct places.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-10-05 17:43
    Ok, Ive looked through the documents and the SELECT...CASE command is the better fit. Here is the edited code

    350cm, 175cm, 85 cm, and 40cm
    'PingCentimeters.BS2.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    time VAR Word
     
    Piezo PIN 2
    Sonar PIN 15
    Durat CON 500
    BFreq CON 4000
    
    
    DO
    PULSOUT Sonar, 5
    PULSIN Sonar, 1, time
    DEBUG HOME, "time = ", DEC5 time
    time = time ** 2251
    DEBUG CR, "Distance = ", DEC4 time, " cm"
    
     
    SELECT time
      CASE 37 TO 43
        FREQOUT Piezo, Durat, BFreq
      CASE 82 TO 88
        FREQOUT Piezo, Durat, BFreq/2
      CASE 172 TO 178
        FREQOUT Piezo, Durat, BFreq/4
      CASE 347 TO 353
        FREQOUT Piezo, Durat, BFreq/8
      CASE ELSE
        PAUSE Durat
    ENDSELECT
    
    LOOP
    
     
    

    This will hopefully work as expected; instead of the exact value, a range is given to give a little more "play" in what is considered a valid distance to trigger the sound, I chose a range of 7 cm for each value, change this as you wish for it to be a tighter or looser range. I have set the Piezo on pin 2, change this to what pin you actually used. The tone will played for a half second, change this value to whatever you want. I chose 4kHz, 2kHz, 1kHz and 500 Hz for the frequencies, change the spread (value BFreq is divided by) and·the base frequency (BFreq) to whatever you want.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • CrystalionxCrystalionx Posts: 18
    edited 2005-10-05 19:50
    I will try this tomorrow and let you know.· I want to express my gratitude for your help and the help of the others.· I hope that I will be able to be of help to others one day on this forum - or elsewhere in the field of electronics technology.
  • CrystalionxCrystalionx Posts: 18
    edited 2005-10-07 17:57
    This code has worked.· We still need to tweak it for our needs, but we have beep?· Allow me to just express my gratitude by saying thank you.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-10-07 18:55
    No problem, glad to help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
Sign In or Register to comment.