I'm having difficulty thinking through the logic of this. I guess I missed statistics at school or something
I want to count the number of pulses from a water flow meter and keep a total of:
Pulses today - that's easy.
The realtime current rate of pulses (e.g. 30/min)
Those two should be easy - I can reset every midnight for 1. and for 2. I can just measure every 10 seconds and add up the pulses but I also want:
No. of Pulses in the last 30 days.
No. Pulses in the previous 30 days
Firstly, am I reinventing the wheel ? Is there a library or ready to run code for this but if not, how do I keep a running total which, every 24 hours, advances to include the new day and exclude the day now 31 days ago ? Does that make sense ?
Secondary question; in a related old topic here, someone mentioned about using interrupts in order to not miss any pulses, but if I use an ESP32 and the code is simple, is the main loop not going to be fast enought to catch every pulse, or is it more a case of the pulses being too fast... I don't know how long the pulses are... milliseconds or much shorter. I found some code to count pulses here so the main question is on the 30 day/60 day totals.
Thanks,
Each day, put the total in an array with 60 levels and advance the array index by 1. When the array index reaches the end of the 60 day period reset it to zero and carry on, overwriting the 60 day old data
At any time you want you can read the value of the last 60 days saved and do whatever you want with it
The post above describes a "circular buffer", an old and very useful concept in computer programming. There is plenty of discussion and example code on line.
Would implement it as an array of 60 unsigned ints, and use the index as the number of days ago.
Every day you start with shifting all numbers one place towards the end (and the last will fall out of the array).
New pulses are always added in arr[0] as that is 0 days ago. After a day the move to arr[1] and after a week they are in arr[7] etc.
possible alternative for 2:
Keep track of the last two pulse moments and calculate the rate from there.
e.g.
if T1= 15015 millis and T2 = 15376 millis, duration == 361 millis => 60000.0/361 = 166.2 / minute.
new pulse => T1 = 15376 T2 = 15699 etc. => 60000.0/323 = 185.8 / minute
Note this is not the exact number of pulses in the last minute, but it is the last measured pulse rate per minute. It can and will fluctuate a bit more but it is as real time as you can get.
rather than shifting all values by 1 array element each time a new one is available, why not use an index to keep track of where the next value is to be written, which also means it over-writes the oldest value.
if you want display value from newest to oldest, start at index-1 (accounting for wraparound and decrement (accounting for wraparound) until the index value is reached
you can also have a size variable to keep track of the # of values in the buffer