Help please Putting Count & Pulsin together.

ernest

Member
Hi all. Help please. (I use Picaxe with my Meccano hobby).
I have a slotted optical switch connected to my CH1035A board 18m2 . the switch works fine.

What am I trying to do.
I am trying to count Revs , using one hole in a rotating disk ,and then do something with the
result.

I am struggling to put count and pulsin together . I have read Pulsin 2/160 & Count 2/50.

COUNT pin, period, wordvariable
pulsin C.3,1,w1 ; record the length of a pulse on C.3 into w1

Thanks Ernest
 

PaulRB

Senior Member
Ernest, can you tell a little more about what you are trying to do?

Do you want to count total number of revolutions completed since a certain point in time, or measure the revolutions per second or per minute?

Either way, you probably want one or the other of those 2 commands but not both.

Paul
 

ernest

Member
Hi Paul thanks for your reply,
I am simply trying to count each hole as it passes the switch.
The switch go’s high as the hole in the disk passes.
I put the switch is high signal into a variable and then count the variable.
The problem is I get multiple switch highs and the incremented variable ‘runs away’ and so the hole count is wrong.
Ernest
 

srnet

Senior Member
What are you using as the 'switch'

Posting the circuit diagram and the code you are using would help everyone to hep you.
 

oracacle

Senior Member
i am not sure why you would need to us pulsin unless you where going to calculate speed, even then that can be done with the count command

however from what i understand it is not a hardware issue, what are you trying to do with the result?

remember if the the whole does not pass the switch within the 50 (not very long at 4mhz) then there will be no reading.

from what i can understand you should have something that reads

Code:
count c.3, 50, b0
pulsin c.3, 1, w1
for more info on pulsin, manual 2, page 160, also dont forget that pulsin will time out at just over 0.6s
 

ernest

Member
Thanks all for your replies. It seems I can simply count holes in a rotating disk without using count or pulsin.
Code:
start:
if pinc.6=1 then goto main
goto start

main:
if pinc.5=1 then goto SwitchMade
goto main

SwitchMade:
inc B1 
if B1=8 then goto me:

SwitchNotmade:
if pinc.5=0 then goto main
goto SwitchNotmade

me:
high 1 'put an Led on for 5 seconds
B1=0
pause 5000
low 1
low 5
goto start
end
Thanks Ernest
 

Goeytex

Senior Member
How you do it all depends upon the application.

...I am trying to count Revs , using one hole in a rotating disk ,and then do something with the result.
This is extremely vague and tells us very little.

How fast is the disk rotating?
Is the speed constant or changing?
What do you want to do with the result ?
How fast do you need the result ?
Can you wait seconds for the result or do you need it quickly?


"Count" simply counts how many times the switch closes / opens over a user defined period of time. If all you are needing to do is count revolutions without the need for speed calculation, then count is a good choice. However with a slowly rotating disk with only one switch actuation per revolution it would be a poor choice for calculating speed.

"Pulsin" measures either the negative or positive time period of a pulse with the maximum measurement period being .65535 seconds with the Picaxe operating at 4MHz. By measuring both the positive and negative pulse widths sequentially and then adding them together you can calculate the speed. Pulsin can also give a count by incrementing a variable inside the main loop.

If you do this "Test" one time with a test program you can calculate the ratio of on time to off time and then by simply using Pulsin to measure the shorter "on time", the time period can be quickly calculated.

For example. A test program is run with the disk at a fixed speed. Pulsin measures the on time as 400 and the off time as 8000. The ratio is then calculated as 8000/400 = 1:20. So now all we need to do to get the time period is to read pulsin once and then do a little math.

Note: This will all work fine as as long as the space time is < .65535 seconds. If the disk speed is slower than that the Picaxe processor speed can be increased, or additional holes can be put in the disk. I prefer to use 4 holes / slots spaced 90 degrees apart. Care must also be taken so that the time it takes to do calculations inside the loop does not exceed the "off time" or ... else the count will be off.

Test Code:
Code:
'*************************************************
;This code only gets mark & space times for calculation of ratio  *
'*************************************************

#picaxe 08M2
#no_data

symbol mark  =   w0
symbol space =   w1 

Main:

do

    pulsin c.3,1,mark        'Get positive width in pulsin units
    pulsin c.3,0, space
    sertxd ("Mark = ",#mark,cr,lf)
    sertxd ("Space = ",#space,cr,lf)  
    pause 1000   
       
loop  

'==========================================================

'With a claculator divide space by mark to get ratio (multiplier)

'With ratio in hand space and period can now be calculated by reading only the mark.
'where space = mark * multiplier .  Use this multiplier this in code blow 
'===========================================================

Example Code:

Code:
#picaxe 08M2
#no_data
;default freq = 4MHz

symbol mark  =   w0
symbol space =   w1 
symbol period =  w2
symbol counter = w3


;Note:  '1 Pulsin unit = 10 us   @4MHz
        '1 Pulsin unit = 5 us    @8Mhz
        '1 Pulsin unit = 2.5 us  @16MHz
        '1 Pulsin unit = 1.25us  @32MHz  
Main:

do

    pulsin c.3,1,mark        'Get positive width in pulsin units
    inc counter              'increment counter 
    space = mark * 20        'calculate space in pulsin uints  (20 is the multiplier)
    period = mark + space / 100    'calculate period in milliseconds
    
    
    rem sertxd ("Mark = ",#mark,cr,lf)
    rem sertxd ("Space = ",#space,cr,lf)
    sertxd ("Period = ",#period," ms",cr,lf)
    sertxd ("Counter = ",#counter,cr,lf,cr,lf)

loop
 

ernest

Member
Goeytex
Thank you kindly for you comprehensive reply.
Your reply is much appreciated I will have a try and see how far I can get with it.
Ernest
 
Top