counter of pulses

Gonçalo

Member
hi my friends. Iam here to ask you how can i make a basic counter program

flowmeter values table: type 5

1.PNG

they give me a x pulses per liter value but in front they give me a frequency. thats what's confusing me. what signal i'm i receiving by the flowmeter?
it sends square signals

i have this code given by Picaxe

count C.1, 5000, w1 ; count pulses in 5secs (at 4MHz)
debug ; display value
goto main ; loop back to start

how can i use to count this.
 

johndk

Senior Member
The way I read your table, the flow meter will click off 450 pulses for each liter. The frequency refers to how fast those pulses are generated. The faster the flow, the faster the pulse rate (frequency). So if you're interested in the total volume, you need to count the pulses. The code you show will do that over a span of 5sec. Increase or decrease the 5000 for longer or shorter times.

If you need to know the rate of flow, then you'll want to work with frequency. I would suggest you change the 5000 to 1000 which will give you the pulses per second ... which is Hz or frequency. Divide your count by 450 and you should have your liters per second.

John
 

Gonçalo

Member
Thanks btw for your answer.

i tryed to aply the code and lcd to read the value.
but it jumps to 255 and does nothing when i press the a.0 input in simulation.

Capturar.PNG
 

lbenson

Senior Member
I have to say that while your code looks good to me, I can't make any sense of the way it runs in simulation.
Code:
#picaxe 08M2 ' 20X2 '

main:
  count C.1, 50000, w1 ; count pulses in 5secs (at 4MHz)
  sertxd("Count: ",#w1, cr,lf)
  goto main
I would have thought that this would sit at the "count" statement for 50 seconds (as I have coded it--same behavior with 5 seconds), and I could click C.1 on and off to get a count. Instead, in simulation with PE 5.5.6, it continually cycles at top speed, and a count of 0 is always printed.

In PE6 it also cycles without pausing at "count", but the number displayed is always 32000.

Didn't try it on a real chip.

I suspect that your max of 255 is the reason you are getting that displayed (but have no idea why w1 should have a value of 32000). By the way, did you mean for your count to be 5ms instead of 5 seconds?
 

hippy

Ex-Staff (retired)
Simulation takes result values for COUNT and similar commands from the Simulation / Values panel in PE6.

In PE5 it would take the value of the "generic" field.

It does it this way because it simply would not be possible to click on an input pin during any short counting period.
 

Gonçalo

Member
Offtopic:

This is the program i've made 2 years ago to count the pulses. the problem is that i have no ideia if it is counting correctly wich i thinks its not but i cant test right now. still its not the crrect form because the pic speed is higher than 500hz and it may count 2 or 3 times the same pulse signal. . it kinda works but not well.

Cell_22_4:
high B.6 .......... .......... .... pump

high B.0 .............. ........ valve
if pinC.3=1 then ...................... pulsecounter

goto Cell_25_3
end if
goto Cell_22_4

Cell_25_3:
let varA = varA + 1 ............... each variable has 8bits so i can only count to 255(as i knew 2 years ago) so i divided the total number of pulses i wanted in variables
if varA = 2 then ............... A, B, C.
goto Cell_28_7
end if
Cell_25_16:
if varB = 30 then
goto Cell_28_16
end if
Cell_25_23:
if varC = varH then
goto Cell_28_23
end if
let varG = varC / 3
bintoascii varG, varTEMPBYTE1, varTEMPBYTE2, varTEMPBYTE3
serout B.7, N2400, (254, 128, "A ENCHER -> ", varTEMPBYTE1,varTEMPBYTE2,varTEMPBYTE3,"L")
bintoascii varI, varTEMPBYTE1, varTEMPBYTE2, varTEMPBYTE3
serout B.7, N2400, (254, 192, "SELEC BAG ", varTEMPBYTE1,varTEMPBYTE2,varTEMPBYTE3,"L")

goto Cell_22_4
 

hippy

Ex-Staff (retired)
The main obstacle here is trying to use flowcharts to perform a task which is not suited to flowchart programming because that has limitations in the size of variables it can handle.

Based on the specification of 450 pulses per litre; these should repeatedly calculate flow rates over second long periods ...

Code:
Do
  Count C.1, 1000, w10
  w11 = w10 * 100 / 45
  BinToAscii w11, b5,b4,b3,b2,b1
  SerTxd( #w10, " pulses ", b4,".",b3,b2,b1, " litres per second", CR, LF )
Loop
Code:
Do
  Count C.1, 1000, w10
  w11 = w10 * 40 / 3
  BinToAscii w11, b5,b4,b3,b2,b1
  SerTxd( #w10, " pulses ", b4,b3,".",b2,b1, " litres per minute", CR, LF )
Loop
 
Last edited:

hippy

Ex-Staff (retired)
It might be worth detailing what exactly it is you want to measure or do; whether to show flow rates, show how much liquid has been delivered, or to stop a pump or sound an alert once a certain amount of liquid has been delivered.
 

Gonçalo

Member
a lil bit of all. the program is for a bag filler. i use a oled lcd. when the pump starts it starts counting, it tells me in the lcd how much it is going.(no need for small numbers, just liters), and after reaching the amount i choose before, it will stop the pump and shut the valve and reset variables.


could you please explain to me this expression?
w11 = w10 * 40 / 3
BinToAscii w11, b5,b4,b3,b2,b1
SerTxd( #w10, " pulses ", b4,b3,".",b2,b1, " litres per minute", CR, LF )

and what's the difference between both codes.
 

hippy

Ex-Staff (retired)
The example code counts pulses over a one second period. Every 450 pulses represents a litre. It calculates how many millilitres flowed in that second ...

mlps = pulses * 1000 / 450

Which can be optimised by dividing multiplier and divisor by 10 to -

mlps = pulses * 100 / 45

That gives us millilitres per second. The millilitres per minute value is 60 times greater -

mlpm = pulses * 60 * 1000 / 450

or -

mlpm = pulses * 60000 / 450

Which can be optimised by dividing multiplier and divisor by 150 to -

mlpm = pulses * 400 / 3

That result could overflow the maximum 16-bit mlpm value allowed, so we divide the 400 by 10 and adjust were the decimal point is in the display, giving -

mlpm = pulses * 40 / 3

If wanting to determine how much has been delivered, it is probably best to do it how you were doing it, counting each pulse and adding 1/450th to a running total -

Code:
pulses = 0
litres = 0
Do
  Do : Loop Until pinC.3 = 0
  Do : Loop Until pinC.3 = 1
  pulses = pulses + 1
  litres = pulses /  450 + litres
  pulses = pulses // 450
Loop Until litres >= wanted
The challenge will be in updating your LCD without losing any pulses. At maximum flow rate there will be a pulse every 1.7ms and at 2400 baud it takes 4ms to send a single character.

You will probably need to use multiple PICAXE devices or a PICAXE which is able to count pulses in hardware, eg SETTIMER COUNT.
 

Gonçalo

Member
Well first of all thanks. you are a genious.
Abou the lcd i will only need the liter value so it wont need to be very precise.
 
Top