PDA

View Full Version : Sound circuit routine



ltmhall
04-13-2007, 11:03 PM
· 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
·
' -----[ Main Routine ]-------------------------------------------------------
DO
GOSUB check_sound1
Loop
' -----[ 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
·

Vern Graner
04-13-2007, 11:33 PM
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..? http://forums.parallax.com/images/smilies/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

Franklin
04-14-2007, 12:09 AM
What are all the elses in there? you don't need them if they don't have any associated statements.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

ltmhall
04-14-2007, 12:55 AM
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 Green
04-14-2007, 12:58 AM
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.

ltmhall
04-18-2007, 11:05 AM
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 Green
04-18-2007, 11:25 AM
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.

ltmhall
04-18-2007, 06:45 PM
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 Green
04-18-2007, 08:53 PM
As far as I can tell, your program does what you describe.