I have taken a keen interest in building on a post made in Dec 2012 by KapteinFredrik(Using two flow sensors connected to one Arduino. - Sensors - Arduino Forum) and have run into a couple of challenges, one of which is compressing code to handle not only UTFT and Wire Libraries but also in providing measurement accuracy. I happen to be using a 480x320 pixel screen and a combination of large font and seven segment font to add to the challenge.
However, my reason for this post stems from the original discussion and the very helpful additions made in August 2013 by "robtillaart" regarding difficulty in keeping accuracy.
I note in the sketch the following bit of code:
void loop()
{
unsigned long now = millis();
if(now - oldTime >= 1000)
{
unsigned long duration = now - oldTime;
oldTime = now;
// store usage of last second in circular buffer
idx++;
if (idx == 60) idx = 0;
// disable interrupts while reading & reset counters.
cli();
circBuf[idx] = countIN - countOUT;
countIN = 0;
countOUT = 0;
sei();
totalPulses += circBuf[idx];
long pulsesLastMinute = 0;
for (uint8_t i=0; i<60; i++)
{
pulsesLastMinute += circBuf*;*
- }*
- // LPH based upon last minute*
float LPH_M = pulsesLastMinute/10000.0 * 60; - // LPH based upon last second*
float LPH_S = circBuf[idx]/10000.0 * 3600; - // Total Liters since start*
- float liters = totalPulses/10000.0;*
In looking at this I wonder about the true sampling time for the counts. A variable calculated but never seemingly used is "duration", the difference between the oldTime and now, ostensibly 1 second but potentially greater than precisely 1000 milliseconds in the "if" statement.
Would it not have been more accurate to divide the (countIN-CountOUT) by the duration and multiply the value by 1000?
I will place another post about squashing libraries to conserve space.
Thanks for the enlightenment.