sparkfun sound detector module test code

Gramps

Senior Member
I'm missing something. Please help.
LED connected to pin C.4
Sound module gate connected to pin C.1

On start up the LED lights.
When sound is detected, LED goes out.
Why doesn't the code loop?

'sparkfun sound detector module test code
code:
#picaxe 08M2
#no_data

symbol _C4=C.4
poke 56,_C4,$ff

bptr=56
do : high @bptr : Pause 100 Do : Loop : Until pinC.1 = 1 : low @bptrinc : Pause 100 : loop
 
Last edited:

oracacle

Senior Member
when you say "LED is connected to C.4". is it connected to the gate output of the detector?

Code:
#picaxe 08M2
#no_data
symbol _C4=C.4
poke 56,_C4,$ff
 bptr=56
 do
  high @bptr
  Pause 100
  Do
  Loop
  Until pinC.1 = 1
  low @bptrinc
  Pause 100
 loop
if you structure your code like thise you can see that the escape from the loop in the middle is outside of the loop for it to exit, and as a result it will be stuck there forever, remove the colon between the loop and until pin c.1 = 1 will enable this escape.

while there isn't anything wrong with your code structure, using the new line for a new command with indentations for each loop allows the code to be more easily read and makes debugging easier, by comparison to a command separator such as the colon like you have done.

this is a short section of code I wrote for a project. the indentations show where each statement or loop is and makes mistakes stand out
Code:
  do
   let led = 60
   gosub display_address     'centre LED always displayed
   bptr = 38
   if min_5_flag = 1 then
    fade_leds       'call macro
    'gosub faded_leds
   end if
   if press_two = 1 then
    if min_5_flag = 1 then
     min_5_flag = 0
    else
     min_5_flag = 1
    end if
   pause 1000
   end if
   if press_one = 1 then exit
  loop
 

AllyCat

Senior Member
Hi,

As above: Do : Loop : Until pinc.1 = 1 throws a syntax error for me.

Rather convoluted code, but I suspect the problem lies in the : low @bptrinc

I believe that will take C.4 low and then increment the pointer to 57 (which contains 0), so leaving C.4 unchanged (at least for the next 127 loops when the bptr should wrap around).

Cheers, Alan..
 
Last edited:

lbenson

Senior Member
bptr=56
do : high @bptr : Pause 100 Do : Loop : Until pinC.1 = 1 : low @bptrinc : Pause 100 : loop
As stated, this does not pass syntax. And as also stated, bptrinc is surely wrong--you probably want just @bptr.

But that also is convoluted--why do you want to use bptr instead of "high C.4" and "low C.4"?

Furthermore, if the execution reaches the final "loop", C.4 will immediately be brought high again even if pinC.1 still is equal to 1.
Perhaps you want something like this:
do : high C.4 : Pause 100 : Do : Loop Until pinC.1 = 1 : low C.4 : Pause 100 : do : loop while pinC.1 = 1: loop
 

AllyCat

Senior Member
Hi,

If you want to write "generic" code to control several pins, then you can use High mypin , etc., where the symbols would be pre-defined, e.g. SYMBOL mypin = b10 : mypin = C.4 .

If the port.pins are consecutive, you could even use INC mypin . I was going to suggest that if not consecutive you could use a LOOKUP mypin (c.4 , c.3 , etc..) but the compiler won't accept it; You'd need to put in the explicit numbers for the port.pins, e.g. from : SERTXD ( #c.4 , #c.3 ) etc. in the Simulator.

It is even possible to use @BPTRINC , but there seems very little point because you would still need a "counter" to exit from the pin-incrementing loop (there aren't 128 or 256 pins on any PICaxe ! ) and all the "target" bytes will need to be defined, e.g. POKE 56 , c.1 : POKE 57 , c.2 : POKE 58 , c.3 : etc..

Cheers, Alan.
 

lbenson

Senior Member
It is even possible to use @BPTRINC , but there seems very little point because you would still need a "counter" to exit from the pin-incrementing loop
I think this code is copied from previous code where a series of different pins would be lit corresponding to the notes in a tune. The "$ff" which was poked with "poke 56,_C4,$ff " would have been the terminator, but this code doesn't check for that. Note that it's not the pin number which is being incremented, but a pointer to the next pin number in a series.

As code for checking whether the Sparkfun sensor is suitable for giving a digital indication on the GATE pin of a note being struck, it might be best to unwind the code into a more readable format, as Oracacle suggests:
Code:
do
  high C.4
  Pause 100
  Do : Loop Until pinC.1 = 1
  low C.4
  Pause 100
  do : loop until pinC.1 = 0
loop
This assumes that pinC.1 starts at zero--perhaps not a valid assumption, but it shouldn't matter for testing. The two "PAUSE 100"s mean that you have a maximum of 1000/200*60 or 300 beats per minute if the loops terminate when first encountered, which seems not unreasonable.
 

oracacle

Senior Member
its maybe helpful to know more about the testing situation.
Seeing as the gate is an on/off signal it can be replaced with a simple push button so code can be checked that way as well.

here's what I might do to check things out, it includes a sertxd for better debugging and comments so that each part of the code can understood

Code:
#picaxe 08M2
#no_data
#terminal 9600

main:
 high c.4   'set output on
 pause 100   'debounce
 
 do    'hold here until pin goes high
 loop until pinc.1 = 1 'exit once high
 
 inc b0   'increment counter
 sertxd (#b0,13,10) 'send count to terminal
 low c.4   'once input high switch off output
 pause 100   'debounce
 
 do    'hold here until input goers low
 loop until pinc.1 = 0 'exit once imput low
 
 goto main   'run again
adding comments to your code is always a good idea, specially when trying to debug or get help with it as it makes things clearer. It also makes it easier for other people to understand specially when they are still trying to learn.

I have attached a print screen, as the code copied doesn't do the formatting any good.
This code does work in simulator so should work with a push switch or the sound detector
 

Attachments

Gramps

Senior Member
bptrinc is surely wrong--you probably want just @bptr.
Yes, we do not have a clue where that came from apparently happened when I copied and pasted to the Forum.
Yes, the line of code is copied from hippy's hand.
Yes, using High instead of bptr is the correct way to go.
Thank you Oracacle 4 this little gem code 2 check the sound detector.!
And thank you for all your advice!
Gramps
 

hippy

Technical Support
Staff member
the line of code is copied from hippy's hand
Quite probably but you are using it out of context. It was intended to activate a sequence of pins from a pin number list held in RAM but you are not using it that way.
 

Gramps

Senior Member
Surprisingly, it does work. When the gate on the sound sensor sends a signal, the LED lights. The problem was it was hanging and would not go out. IBenson spotted the reason why it hangs.
 
Top