# Do task according to pulse count or frequency

Hi

I am trying to make a program for a picaxe 20m2 chip such that it continuously counts pulses in 100 milli seconds and do specific tasks according to the no. of pulses, for ex- light an LED connected on pin b.0 for 1 second when the pulse count is 100 and light another LED which is connected to pin b.1 when the pulse count is 200. it should be like that it ''continuously monitors'' the pulses coming on one of the pins (say c.7) and do tasks accordingly but I have no idea how to do it.

Actually, i want my picaxe to respond to audio signals which contains only one single frequency (like 1000 Hz at one time) coming from a source (say a mobile phone's audio jack) and do what i have programmed it to do when it gets a particular frequency (1000Hz ---> light an LED on pin b.0).

WHAT I HAVE DID TILL NOW :-

I played a 1000 Hz tone from my phone, converted it to 5 volt digital pulses(sort of) using an op amp as they were analog audio signals (which i think can't be counted by picaxe) when they came out from my phone's audio jack, made my picaxe count those pulses using this program-

main:
count c.7, 100, w1     ; count pulses in 100 milli secs (at 4MHz)
debug                       ; display value
goto main                         ; loop back to start

And success! (means my crappy digital pulse convertor(sort of) worked! ) It worked perfectly and it showed the value '100' in w1 in the debug window. After it, I tried 2000 Hz and it showed the value '200' indebug window.

Now i want to write a program so that it do different things on different frequencies but i don't have any idea about the code, so i want some help.

Thank you :)

## Comment viewing options

My suggestion is to do two things:

1) Convert the counted pulses to nearest number of 100s with these two lines of code:
w2 = w1 + 50
w2 = w2/100
After running the above, w2 will contain 0 if w1 was 49 or less, 1 if w1 was between 50 and 149, 2 if w1 was between 150 and 249, etc, etc...

2) Use select/case control to perform the different actions:
select case w2
case 0
;action #1 goes here
case 1
;action #2 goes here
(fill this in with however many cases you need)
else
;do this action if none of the cases match, optional
end select

actually i am using frequencies- 1000 Hz, 2000 Hz and 3000 Hz so i got exact 100, 200 and 300 reading on w1, so no need to convert them into hundreds as they are already. I read the select and case command in picaxe manual 2 and learned it was so simple. It is working perfectly now! this is my code finally-

main:
do
count c.7, 100, w1
select w1
case 98 to 102
high b.0
low b.1
pause 3000
case 198 to 202
high b.2
low b.3
pause 3000
else
low b.0
low b.1
low b.2
low b.3
endselect
loop

I always forget you can use an expression (like "98 to 102") for a case statement in some languages, looks like you came up with a nice solution to your problem that incorporates some tolerance in your count value =)

If you eventually make your different actions more complicated, you should look into creating some functions/sub-routines/sub-procedures to tidy up your code, such as:

...
case 98 to 102
gosub led1
case 198 to 202
...

led1:
high b.0
low b.1
pause 3000
return

This isn't a big deal for turning on and off a few outputs with a few pauses thrown in, but if things start to get complex, or you want to repeat the same action at different points in the program, it can be really helpful.