A little help with logic please

Hi, I have water being supplied to a float-valve. I want to measure the GPM and have added a flow meter with F=(38*Q) the float-valve so I can calculate the GPM.

But I need to know when the float-valve has opened and when it shuts off. Here's where I am at the moment, but cannot seem to get it to do what I need. I am old and the brain is just fuzzing over with this one. :slight_smile:

volatile int flowTotal = 0;

int flowLast   = 0;
int flowsensor = 2;

bool isRunning = false;

void flow () {

void calcGPM() {
  // I can do this stuff

void setup() {
  digitalWrite(flowsensor,HIGH); // Optional Internal Pull-Up
  attachInterrupt(0,flowsensor,RISING); // Setup Interrupt
  sei(); // Enable interrupts

void loop() {
// here is where I am stuck
  if (flowTotal == flowLast) {
    isRunning = false;
    flowLast = 0;
    flowTotal = 0;
  } else {
    isRunning = true;
    flowLast = flowTotal;
  if (isRunning) {
    Serial.print(", ");

A great deal of essential information is missing from your post.

Read it over, pretending you are clueless about the topic, and try to guess what that might be.

The second argument is the interrupt service routine name, not the pin number.

But otherwise the loop() looks OK. Or what is wrong with it? However, the interrupt service routine calcGPM() , which you have indicated you can do yourself, needs some work because it has to also store the time of the last activation so you can do the flow rate calculation. This requires either an additional global variable or a static variable which is local to the routine.

Presumably you have to judge this from the flow-rate? Perhaps just timeout the flow pulses to determine when it closes, and the next pulse indicates its opened up again.

What minimum pulse rate does the flow meter give?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.