Sound circuit routine
ltmhall
Posts: 102
· 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
·
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
and then later this line:
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..?
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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
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
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.
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.