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;

// INTERRUPT ROUTINE
void flow () {
  flowTotal++;
}

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

void setup() {
  pinMode(flowsensor,INPUT);
  digitalWrite(flowsensor,HIGH); // Optional Internal Pull-Up
  Serial.begin(115200);
  attachInterrupt(0,flowsensor,RISING); // Setup Interrupt
  sei(); // Enable interrupts
}

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

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.

Edit
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?