Shop OBEX P1 Docs P2 Docs Learn Events
Sound circuit routine — Parallax Forums

Sound circuit routine

ltmhallltmhall Posts: 102
edited 2007-04-18 12:53 in BASIC Stamp
· I'm writing a program that will listen for a sound and when it detects it pauses for a few seconds
and checks to see if the sound is still there. I have written a routine and I wanted to know if there is
·an easier way to write it using less code than I have.


detectsound········· VAR ·····Bit
count····················· VAR ·····Bit
pulseCount···· VAR···· Byte
·
'
[noparse][[/noparse] Main Routine ]

DO
GOSUB check_sound1
Loop
'
[noparse][[/noparse] Subroutines ]

check_sound1: ···············································‘ Program to check sound
count = 0·· ······················································‘set value of count to zero···············
IF (detectsound = 1) Then·············· ················‘ if detect sound detect sound = 1
count =·1··········· ··············································‘ adds 1 to the value of count
PAUSE 3000······································· ············‘ pause for a short time
GOTO check_sound2························· ·············‘ now ready to check if sound is still there
ELSE····················· ··········································‘ if no sound found return
END IF
Return
·
check_sound2····················································· ·‘checks again for sound
If (detectsound =1) Then· ································‘ if sound found again detectsound = 1
count = 1+count··············· ································‘ increments count by one again
Goto found_sound············ ·······························‘ found sound
Else··································· ·······························‘ if not return
Endif
return
·
found_sound:························ ····························‘found sound
If (count > 2) Then ············································‘ one more check for sound
GOSUB Backwards
GOSUB Backwards
GOSUB Right
Else
Return
·
Back_Up: ·······························' Back up.
FOR pulseCount = 0 TO 40
PULSOUT 13, 650
PULSOUT 12, 850
PAUSE 20
NEXT
·
Turn_Right:
FOR pulseCount = 0 TO 20 ···············' Right turn, about 90-degrees.
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 20
NEXT
RETURN
·

Comments

  • Vern GranerVern Graner Posts: 337
    edited 2007-04-13 15:33
    ltmhall said...
    I have written a routine and I wanted to know if there is an easier way to write it using less code than I have.
    Right off the bat, I see a problem with this variable:

    count    VAR      Bit
    
    


    and then later this line:
    count = 1+count   ‘ increments count by one again
    
    


    Since COUNT is a BIT it can only hold a value of 1 or 0. This would have to be redefined as a nibble, byte or word (depending on how large you plan to let it grow).

    I also notice you call a subroutine called "Backwards" that does not appear to be present in the source code you provided..? confused.gif

    Another possible optimization would be to make one routine to detect sound, rather than two routines. Just call the subroutine that detects sound each time you need to.

    Anyway, thats just off the top of my head... maybe post your entire program and then tell us a bit more about what you're trying to accomplish. You didn't say so, but I assume from the code that you are trying to control a BOEBOT by having it sense sound?

    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
    
    
  • FranklinFranklin Posts: 4,747
    edited 2007-04-13 16:09
    What are all the elses in there? you don't need them if they don't have any associated statements.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • ltmhallltmhall Posts: 102
    edited 2007-04-13 16:55
    The back_up and backwards commands aren't really important I never intended to use this particular program. I'm more
    concerned with the subroutines for detecting sound.

    Someone said " I don't need the else commands", but I thought I had to add a else statement when I use if/endif
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-13 16:58
    Reread the PBasic manual's section on the IF statement. You don't need the ELSE if there are no statements between the ELSE and ENDIF.
  • ltmhallltmhall Posts: 102
    edited 2007-04-18 03:05
    I rewrote the program and I'm still having problems. Does anyone have any suggestions ?
    I'm not sure if I'm defining my variable "mic" right !


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    detectsound VAR Bit 'define sound
    mic VAR Word ' variable use to store bits
    pulseCount VAR Byte
    main routine

    mic = 0 ' set variable initially to zero
    DO
    GOSUB check_sound ' go and check for sound
    PAUSE 300 ' pause
    GOSUB check_sound ' check sound again
    IF (mic > 1) THEN ' if sound still there manuever
    GOSUB backwards
    ENDIF
    LOOP

    subroutine
    (check_sound)
    check_sound:
    detectsound =IN6 ' sound pin 6
    IF ( detectsound = 1) THEN ' check if here sound
    mic = mic + 1 ' if sound heard add 1 to valur mic
    ENDIF
    RETURN

    subroutine
    (others)
    backwards:
    FOR pulsecount = 0 TO 40
    PULSOUT 15, 650
    PULSOUT 14, 850
    PAUSE 20
    NEXT
    RETURN

    right:
    FOR pulsecount = 0 TO 20
    PULSOUT 15, 850
    PULSOUT 14, 850
    PAUSE 20
    NEXT
    RETURN

    Post Edited (ltmhall) : 4/18/2007 10:46:29 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-18 03:25
    I don't see any place where you reset "mic". Maybe you should do that after the "GOSUB backwards".
    I assume you are actually getting IN6 set to high when you make noise. Have you checked that?

    When you say that you're having problems. It's really very helpful to describe exactly what seems to be happening,
    otherwise we have to guess ... and we might not be right.
  • ltmhallltmhall Posts: 102
    edited 2007-04-18 10:45
    What I want the program to do is to check for noise and when it hears a noise it sets mic = 1, then it pauses and checks
    for noise again, if noise found, adds another 1 to mic. Then it test if the value of mic is greater than 1 and executes the
    backward and right command. What I want to do is write a program that listens for a continous beep and at the same time
    filters out random noise.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-18 12:53
    As far as I can tell, your program does what you describe.
Sign In or Register to comment.