Some help with Math please

I have this to calculate some water flow. I just pumped 1000 gallons exactly and I have an error (using dipstick) of plus 132gallons . What do I need to change the calibration factor to in order to correct the error?

Or at least, get it a lot closer to 1000, a couple of gallons either side is not going to matter.

const float calibrationFactor = 0.20;  // Displays 1132 instead of 1000 actual

void calc() {
  float flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  oldTime = millis();
  pulseCount = 0;
  Serial. println(flowrate,2);
}

Thanks

Well, you want a smaller answer. Since you’re dividing by calibrationFactor, that means you want it to be larger. So, how about:

const float calibrationFactor = 0.20 * (1132.0 / 1000.0);

or

const float calibrationFactor = 0.2264;

gfvalvo:
Well, you want a smaller answer. Since you’re dividing by calibrationFactor, that means you want it to be larger.

Thanks heaps.
{grin} Easy for you to say.

Before posting I was messing around with percentages and getting nowhere fast. You make it look so simple, but I just could not seem to get my head around it. Too old I guess. I appreciate the quick help.

In your code, I see a variable called flowRate, but I don't see anything for keeping track of the total number of gallons which have flowed.

I'm just wondering: how did you arrive at the conclusion that the number of gallons was wrong? What does the rest of your code look like?

odometer:
I'm just wondering: how did you arrive at the conclusion that the number of gallons was wrong?

Good point and thanks for your interest. I copied the code from somewhere and cannot find the link to it anymore. I spent almost two days to find an example that actually worked. I trusted the originator of this as they sounded like they knew what they were doing and I clearly do not. :slight_smile:

I had tested the system using a hair dryer and the resulting GPM and totals over a timed 1-minute, seemed to match up. The water driving the thing in real-time yesterday may have had an effect, but I was not able to dipstick test the volume with the hair dryer. :wink:

But the dipstick in the tank does not lie. Kinda cracks me up that I am messing with all this technology and confirming it with a stick, a tape measure and Sharpie marks.

By the way, there are two flow sensors side by side with "Y" manifolds as the max GPM rate was too high for one. I'd appreciate comments on the "gallonsPerMin" below as that was my thinking for the two sensors.

void checkFlow() {
  if((millis() - oldTime) > 1000) {
    noInterrupts();
float pulseCount = pulseCount1 + pulseCount2;
float flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
float flowMilliLitres = (flowRate / 60) * 1000;
      totalMilliLitres += flowMilliLitres;
      pulseCount1 = 0;
      pulseCount2 = 0;
      oldTime = millis();
    interrupts();
    gallonsPerMin = (flowRate * 0.2642) / 2.0; // We are summing the pulse counts from 2 sensors, but not sure about the validity of this
    totalGallons = round(totalMilliLitres * 0.00026);
    showGPM();
    showGallons();
  }
}

WaitSome:

  if((millis() - oldTime) > 1000) {

I think ">1000" is not correct. This if, is activated at 1001 (and up)
Also, since you use a "no interrupts" your millis() perhaps is not accurate

float flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
float flowMilliLitres = (flowRate / 60) * 1000;

Instead of attempting to first calculate a flow rate and then calculate a volume, try first calculating the volume and then calculating the flow rate. You will need to change the formula, of course.

That number 60 in the calculation for flowMilliLitres looks fishy. In what units of measure is flowRate supposed to be?

Are both sensors the same kind? Should you be using the same calibration factor for both of them, or two different calibration factors?

What kind of sensors are you using?

Perhaps the problem lies elsewhere in your sketch. Please post the entire sketch.

Like this, but bigger with the F = 0.2Q where this one shows F = 4.8Q

Yes they are identical and the pulse counts are always within 1 or 2 of each other per 1-second loop. Near enough for what we need, that's why I am happy enough with a single fudge factor hence asking about adjusting the 0.2.

The two (one per sensor) interrupt routines just increment the pulse counters that are then zeroed every 1-second loop. I can't post the entire sketch, not mine to do so, just trying to help a friend who had this built by a "professional" and it was wildly inaccurate. :slight_smile:

All the calculations are taking place in that function I posted.

WaitSome:
I can't post the entire sketch, not mine to do so, just trying to help a friend who had this built by a "professional" and it was wildly inaccurate. :slight_smile:

What leads you to believe that the sketch is "not yours" to post?
If it's not yours, then whose is it?
Was it the one from this link?

Near the top of the code, I see this:

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version. http://www.gnu.org/licenses/

Anyway, there are serious problems with the code at that link.

Just the idea of "pulses per second per liter/minute of flow" makes my head hurt.

And, the pulseCount in the code at that link was declared as a byte. This means that, if it ever gets higher than 255, it will then start over again from zero, just like a car odometer rolling over. Nothing wrong with that, it's just that you have to check it before the count of pulses has a chance to get that high (maybe twice per second rather than once per second)? Perhaps your use of two sensors, and two pulse counts, works around this limitation.

As for one of your modifications:

    gallonsPerMin = (flowRate * 0.2642) / 2.0; // We are summing the pulse counts from 2 sensors, but not sure about the validity of this

No.
Indeed, you are summing the pulse counts from two sensors. And then, once you have that sum, you should then proceed as though that sum were the pulse count from a single, big sensor. (Which, in a way, it is.)

I suggest that your friend ask for his/her money back from that "professional".

For the sake of my mental health, I am leaving this thread.

odometer:
What leads you to believe that the sketch is "not yours" to post?

Perhaps if you read your own post, the knee need not jerk so hard.

Perhaps the problem lies elsewhere in your sketch. Please post the entire sketch.

I say again, the entire Sketch is not mine to post.

odometer:
I suggest that your friend ask for his/her money back from that "professional".

That's not what friends do. Obviously it is possible to make this work, so a good friend does just not throw their hands up and quit at the first obstacle.

WaitSome:
Perhaps if you read your own post, the knee need not jerk so hard.

Perhaps the problem lies elsewhere in your sketch. Please post the entire sketch.

I say again, the entire Sketch is not mine to post.

That's not what friends do. Obviously it is possible to make this work, so a good friend does just not throw their hands up and quit at the first obstacle.

hee hee that was a play.Right

I had a go at tidying it up so I could follow it. In breaking it down the frequency looks confusing (2 things at 40 hz isn't really 80hz), but I wanted to see flowrate = freq / 0.2 in the code. Overall I can't see a mistake except for the divide by 2 giving a bad GPM output. The gallons should be OK.

But, this code is definitely the wrong approach, every second it stops watching what's going through the pipes so that it can tally up the total so far. The code as shown in the pause isn't enough to explain the 13% error but maybe there is more stuff in there we haven't been shown.

You really just need to count the total number of pulses to get the total volume. And sample the frequency if you want a live readout of the flow rate.

Run some numbers:

full flow rate = 2x 200 = 400 l/min
volume = 1000 usgal = 3785 l
so 10mins pumping

freq = 0.2 * 200 = 40hz = 40 pulse per second (each meter)
x10min = 24000 pulses (no problem with big numbers)

anyway, this is my tidy up
note:this hasn't been anywhere near the IDE as isn't meant to be runnable code:

const float pumpFactor = 0.20;  // 
const float callibrateFactor = 1.00;  //
const int  sampletime = 1000;  // ms
const float litre_to_gallon= 0.264172; //  

void checkFlow() {
  
  if((millis() - oldTime) > sampletime) {
    noInterrupts();
    stoptime = millis(); 
    period = (stoptime - oldTime)/1000; // seconds
 float pulseCount = pulseCount1 + pulseCount2; // this is OK but a bit confusing 
 float frequency =  pulseCount / period; // confusing! 
 float flowRate =  frequency / pumpFactor; // litre/min
 float flowLitres = flowRate / 60; //  litre/sec  
      totalLitres += flowLitres * period ; // 
      pulseCount1 = 0;
      pulseCount2 = 0;
      oldTime = millis();
    interrupts();
    gallonsPerMin = flowRate * litre_to_gallon; // 
    totalGallons = round(totalLitres * litre_to_gallon);
    showGPM();
    showGallons();
  }
}
  stoptime = millis;

Oops

Please use code tags when posting code

TheMemberFormerlyKnownAsAWOL:

  stoptime = millis;

Oops

Please use code tags when posting code

It's pseudo code :slight_smile:

I don't know what code tags are, I'll have a look.

So, use pseudo code tags.

I’d say you’re spending too long with interrupts disabled - copy the variables, reenable, then do the arithmetic.