I have a 2-wire flowmeter that changes output between open and closed every 10 L(itres) of water volume that goes through. E.g. 0-10L open circuit, 10-20L closed circuit, 20-30L open circuit...
Therefore I have it connected to a GPIO and have enabled the internal pull-up resistor.
The flowmeter is being used to monitor and log household usage and is connected to the main line into the house. I have written some code which attaches an interrupt to the GPIO the flowmeter is connected to, and every pulse I detect will add the current time and the pulse volume (10L) into a list of objects that each contain a datetime and volume variable. I keep this list trimmed to x samples (set to 5 at the moment).
So for example, the list might contain (pretend the object is simply its own list and that the date part of the date is included but excluded here for ease of notation):
[10:00:00, 10]
[10:03:00, 10]
[10:04:30, 10]
[10:06:15, 10]
[10:08:00, 10]
Calculating the flowrate is easy, just sum the volumes, get the difference in max and min dates in minutes, and divide:
40L/8min = 5 L/min (exclude the first volume as this applied to the previous period)
Where I'm stuck is for this scenario:
[10:00:00, 10]
[10:03:00, 10]
[10:04:30, 10]
[10:06:15, 10]
[11:08:00, 10]
The same method works, but it's not what you expect, as it's no longer an instantaneous flowrate, but an average-over-longer-time flowrate.
And this is my issue; how do I calculate the flowrate as you would expect to see it? and when the flow stops and the interrupts therefore stop, how do I know when to drop the pulses from the list and update the flowrate?