How do I Count Pulses?

DAK

New Member
I recently built a circuit that would count a predetermined amount of pulses then send a high to an alarm. It is used on a pitching machine. It counts the amount of balls (each time the arm would go around it would trigger the limit switch) and activate an alarm after 8 pitches. Below hopefully gives you an idea of how it is currently set up.
9vdc Power Supply – Supplied power to this circuit

1. Neg. Pulse from Limit Switch
2. Into 556 Timer to debounce the signal
3. The 556 then sent a pos pulse to a 4017 Counter/Divider
4. After 8 pulses or high signals the corresponding pin would go high
5. Then into the other half of the 556 timer (used for the on time of alarm)
6. Then into a Transistor which powered the Alarm. (Radio Shack 273-079 6-14vdc Alarm) This would signal the batter that he was done.
7. A on/off Switch was place by the batter and would turn on or off power to the circuit 9vdc which would reset it to start counting pulses once again.
8. Also the on/off switch to the pitching machine controlled power to the circuit. This would also reset the counter / divider in the event that the machine was turned off.

Because of my limited experience, the board took forever to put together to actually work. I need another circuit for another pitching machine, but don’t want to smoke any more components and spend months on doing so. By the way this board was working for 2 seasons, but recently can only count the correct pulses when it receives them quickly. The arm takes awhile to complete its revolution which in-turn sets the alarm off at different intervals. I changed out all three chips, but now luck. Oh well, maybe I can take care of all of this using a microcontroller like the picaxe 08M. This may be a good time to get my feet wet and look into this possibility.

Now the question is, with my limited knowledge of programming what would it take as far as code to do this? I’m using this microcontroller (PIC12F683) and already have the PICAXE, hardware and programming system which runs on Basic. I have written a few lines of code to turn on LED’s and some other things, but need help writing something that would work to operate the above project. I am stupid ok a little slow, and would need a step by step code and maybe a schematic to accomplish this. How many components would I still need? I know I’m asking a lot, but would appreciate any help I can get.

PIN OUTS FOR THE PICAXE O8M (PIC12F683)
1 = V+
2 = Serial In
3 = ADC4/ Out 4/ In 4
4 = Infrain /In 3
5 = In 2 / Out 2 /ADC 2 / pwm 2 / tune
6 = In 1 / Out 1/ ADC 1
7 = Out 0 /Serial Out / Infraout
8 = 0V

Thank You for Your Time
DAK
 

1968neil

Senior Member
Hi Dak,

This may get you on the right track, basically it waits for a switch to go low (tied high by 10k).
Everytime the switch is pressed it increments a counter upto 8 if it reaches 8 it activates the relay of whatever you want to control.
It then waits for 3 seconds and resets back to zero. and waits to do it all again.
each press selects a branch so you could use it to other things like leds etc. i've commented the code for you.




Code:
symbol relay = 1           'relay on output 1 (pin 6 of chip)
symbol pbswitch = pin3     ' push button switch on pin3 (pin 4 of chip)
symbol down = 0            ' define input state
symbol counter = b0

counter = 0                'set counter to 0 before starting


loop1:

if pbswitch = down then switchdown
branch counter, (One,Two,Three,Four,Five,Six,Seven,Eight)
counter =0       'reset counter
goto loop1

switchdown:
counter = counter +1 'add 1 to count everytime switch is pressed
sdloop:
if pbswitch = down then sdloop  'loop until switch released (debounce in software)
goto loop1

One:
sertxd("1")           ' this commands sends the number 1 to the serial port for debuging
goto loop1

Two:
sertxd("2") 
goto loop1

Three:
sertxd("3")
goto loop1 

Four:
sertxd("4")
goto loop1 

Five:
sertxd("5")
goto loop1 

Six:
sertxd("6")
goto loop1 

Seven:
sertxd("7")
goto loop1 

Eight:
sertxd("8")
high relay               'turn relay on
tune 2, 3,($04,$07,$09)  'play tune on output 2 (pin 5 of chip)
pause 3000               'pause for 3 seconds
low relay                'turn relay off
counter = 0              ' reset counter
goto loop1
 
Last edited:

DAK

New Member
Pulse Counting Works Fantastic

WOW! That’s it?

You’re amazing I went through the simulation mode and it works fantastic. I wish I had this along time ago. Do you know how much smoke it will save my lungs by minimizing the circuitry that was used without using the picaxe? The notes in your code explained everything in a way I could understand it.

Thank You So Much For Taking The Tim To Answer My Post

DAK
 

1968neil

Senior Member
Glad to be of service, if you need any further help just ask.
You'll find a host of like minded people here, enjoy.

Regards
Neil
 

russbow

Senior Member
Be very careful.

The code implies a relay on output 1. Please read
manual 3, page 13 if you want to interface a relay to a Picaxe pin.
Otherwise magic smoke :eek:
 

1968neil

Senior Member
sorry, did it in a hurry !
indeed the programming resistors need updating and the link to ground needs removing, ill update it tonight when i get in from work.

the relay was replaced with an led for testing, a 1k resistor and a transistor will be required for switching a relay.

regards


Neil
 

westaust55

Moderator
Here is an alternative program which does the same thing but using interrupts.

While not necessarily a significant point in this application, an advantage of the interrupt method is that the program can be doing something else rather than just going around in a loop polling the one input.

For example you can add other functions at the line:
; do any other commands here for other functionality

Code:
symbol relay = 1           'relay on output 1 (pin 6 of chip)
symbol pbswitch = pin3     ' push button switch on pin3 (pin 4 of chip)
symbol down = 0            ' define input state
symbol counter = b0

Init:
	counter = 0                'set counter to 0 before starting
	SETINT %00000, %01000

Main:
	IF counter = 8 THEN GOSUB alert

	; do any other commands here for other functionality
	
	GOTO Main
	
alert:
	HIGH relay               'turn relay on
	TUNE 2, 3,($04,$07,$09)  'play tune on output 2 (pin 5 of chip)
	PAUSE 3000               'pause for 3 seconds
	LOW relay                'turn relay off
	COUNTER = 0              ' reset counter
	RETURN

interrupt:
	inc counter
stilldown:	
	IF pbswitch = down THEN stilldown
	SETINT %00000, %01000
	RETURN
all the hardware related comments mentioned by others about driving the relay etc are still valid.
 

MartinM57

Moderator
I think the alleged debounce code in post #2 is faulty (it doesn't actually debounce) and there isn't any debouncing in the interrupt: version.

I'm personally not keen on detecting button presses in interrupts unless there is either or both
a) more-than-adequate hardware debouncing
b) proper software debouncing - which is normally, outside of PICAXEs (although it could be done in PICAXE also), a timer interrupt that polls the switches and only declares a press or release after a certain number of non-changing states are detected
 

1968neil

Senior Member
Hi all,

Martin, agree but the idea was to keep the set up simple as the recipient was a "Newbie".
Test for press then release whilst not technically debounce does the job in most cases in my experience although i usually also fit a capacitor.
but i do understand your point ! :)
Westy :

G'day,
Like the code much simpler than mine, Nice one !

Note: have amended the breadboard layout in post 5 for forum scrutiny ! Hope i got it right this time :D

Regards to all

Neil
 

MartinM57

Moderator
...trouble is even Newbies have contact bounce (and S**s Law says they will have it more than someone who's been around the block a few times!) and if two balls have gone through, but the counter reads 7 already, then confusion abounds :)

Sometimes best to tell people like it is :D
 
Top