Measure Dispensed Water, repeat with push button

Hi, glad it’s working for you.

A couple notes about your final code:

  • Get rid of the ‘oldTime = millis();’ line. Updating of ‘OldTime’ is handled with the ‘oldTime += samplePeriod;’ line:
    if (period >= samplePeriod) {
        noInterrupts();
        currentCount = pulseCount;
        pulseCount = 0;
        interrupts();
        oldTime += samplePeriod;  // <--------------- KEEP THIS
        //
        flowRate = (1000.0 / period * currentCount) / calibrationFactor;
        oldTime = millis();  // <----------------------- GET RID OF THIS
        flowMilliLitres = (flowRate / 60) * 1000;
  • Your computation of quantity dispensed over each ~1 second period is rather convoluted. You convert pulses (which are a measurement of quantity) to a flow RATE which you then convert back to a quantity. That’s like going around your ear to scratch your elbow.

The problem starts with your choice of calibration factor:

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;

Those are truly bizarre units. I assume the flow sensor is a spinning thingy exposed to the fluid’s flow. So, the number of pulses is directly proportional to the number of mL that have flowed passed the sensor. Rearranging your constant with some good old-fashioned "factor-label" conversion, I get:

const float calibrationFactor = 3.7037;   //     Units are mL / pulse;

Note the use of ‘const’. That number is not going to change while your program runs.

So now, multiplying ‘currentCount’ by ‘calibrationFactor’ directly gives you the number of mL that flowed during the last measurement period. Divide that by ‘period’ and you get the flow RATE for that period. That’s much more straight forward.

  • Instead of working so hard to print the float values, check out dtostrf()

Enjoy, have fun with your future Arduino projects!!!