Interrupt Question

I have this basic flow meter setup. It uses two cheap hall effect sensors, which detect the flow. I just got the code from an example and it uses interrupt calls, which I am very unfamiliar with. Actually, I understand the concept, but I cannot wrap my brain around the gritty details. I have it all setup and it reads the flow fine and spits it out the serial monitor and now I am ready to push on to some more stuff. So....

I would like to light up an led only when the sensor is rotating indicating flow. I would like to do it through code, so I can hopefully learn to do some other stuff later on, besides just turning on an indicator light.

I dont understand if its something that should be done inside of that "float getFlowRate(int interrupt)" function, like do a digitalwrite() to cycle the led. Or is there something outside of the function that can be used to say, hey, there is an interrupt taking place.

I am kinda guessing that anything I do inside of the interrupt function will cause some kind of timing issue with the water flow measurement, but thats ok, I am not looking for that kind of precision.

I actually thought that maybe I could just digitalread() the flow sensor and while it was changing states, that tells me it has flow and turn on the led, but it has my head spinning in circles. I am not that creative yet & cant figure out how to put it together. I was thinking maybe I could set it up like having a current read state & previous state and if current state doesnt match previous state then its flowing. But, I am confused.... I searched the web and it seems no one is reading those sensors like that and they are using interrupts.

Anyway... Could someone point me in the right direction on how to turn on a led to indicate that an interrupt is in progress? And maybe giving me some idea of what to look into to just indicating that the sensor is moving, in a situation where I dont want to count flow, just show there is flow.

int ftPin = 4;
int gbPin = 5;

volatile int pulseCount;

float calFactor = 4.8;
float convFactor = 3.78541178; // 1 US gallon per minute = 3.78541178 liters per minute
float flowRate; 
unsigned int frac;

void increment() 
{ 
  pulseCount++;
} 

float getFlowRate(int interrupt)
{
  pulseCount = 0;
  attachInterrupt(interrupt, increment, RISING);
  sei();          //Enables interrupts
  delay (1000);   //Wait 1 second
  cli();          //Disable interrupts
  detachInterrupt(interrupt);
  return ((pulseCount / calFactor) / convFactor); // convert to GPM
  
}

void setup()
{ 
  pinMode(ftPin, INPUT);
  pinMode(gbPin, INPUT);
  
  Serial.begin(9600);
} 

void loop ()    
{
  flowRate = getFlowRate(ftPin);

  
  Serial.print("FT: ");
  Serial.print(int(flowRate), DEC);
  Serial.print(".");
  frac = (flowRate - int(flowRate)) * 10;
  Serial.print(frac, DEC);
  Serial.print(" GPM     ");
  
  flowRate = getFlowRate(gbPin);

  
  Serial.print("GB: ");
  Serial.print(int(flowRate), DEC);
  Serial.print(".");
  frac = (flowRate - int(flowRate)) * 10;
  Serial.print(frac, DEC);
  Serial.println(" GPM   ");
}

Rob

I don't see where Serial.h is included.
Where is "interrupt" declared?
Post all of the code.

There is not a reason in the world for that code to be using interrupts at all. It appears to have been written by someone who did not understand the whole concept of interrupts, and why, and how, they're used. Get rid of the interrupt, and simply poll the flow meter input during the one second you're counting. You'll get the exact same result, without wasting time handling interrupts. OTOH, if you ARE going to use interrupts, write the code so it's not simply spinning it's wheels in a delay while you're doing the counting, so you can be doing something useful instead.

Regards,
Ray L.

Your sensor rotates when you have a flow; so can't you use flowrate to light a led?

In it's simplest form

...
...
const byte ledPin = 9;


// your functions here
...
...


void setup()
{
  ...
  ...
}


void loop ()
{
  flowRate = getFlowRate(ftPin);

  if((int)flowRate == 0)
  {
    digitaWrite(ledPin, LOW);
  }
  else
  {
    digitaWrite(ledPin, HIGH);
  }

  ...
  ...
}

The code casts the flowRate to an int so you can use a simpler compare in the if

You can also light the led proportional using analogWrite().

There is nothing inappropriate about using interrupts to count pulses from the flow meter. But the way @Rob_O is doing it is very complicated. All it needs is an attachInterrupt() is setup() and an ISR something like this

void countPulses() {
   pulseCount ++;
}

Then the code in loop() can read the count value at any time with code like this

noInterrupts();
  latestPulseCount = pulseCount;
interrupts()

and then it can do whatever calculations are needed using the value in latestPulseCount. Make all the pulse counting variables unsigned long.

...R

boolrules, that is the whole code and it works to read the flow meters and outputs it to the serial monitor. I got it off and example from a project where a guy was reading two flow meters for a aquarium or something. It was on youtube somewhere. I copied it, made a few little changes to my setup compiled and uploaded it and its counting pulses.

Ray L, it may have been written by someone who didn't know how to use interrupts. I explained in the post that I sure don't know, so I didn't even understand that it was being misused...

sterretje, now that's what I needed to get the led on. I couldn't figure out how I should code it, but I will start with that, I think that what I need.

Thanks for all of the Replys!

What is the best way for me to learn how to use the interrupts correctly for my sort of application? I try to edit it like Ray suggests and just count it during that one second of counting. But, I really need to understand how to use the interrupts correctly. So any help would be appreciated.

Do you really need to count? If you just need to know if it's flowing or not flowing, then have the interrupt store the value of millis() in a variable. Then see how old the last pulse was - if it was more than X milliseconds ago then there's no flow.

MorganS, in this project, I don't necessarily have to know how much flow there is, so no. So far, that's above my level, but I will look into that. That's a good topic for me to research because I am needing to learn how to deal with time a little more anyway.

What is the MINIMUM flow rate that you you want to interpret as "the water is running", 1 liter per minute? 1 liter per hour?

Rob_O:
But, I really need to understand how to use the interrupts correctly. So any help would be appreciated.

See Reply #4 for starters.

...R

756E6C, not so much a flow rate, but if it is currently changing states on the flow sensor, enable the led. Say a single state change would momentarily blink the led. That is my thinking anyway, I will have to get to some trial and error and see what I can come up with.

Robin2, I did see your posts too, I am gonna do some looking into that and see what I can do....

I do appreciate all the replys, I will do some more editing and some more studying from your comments....

Ray L. & sterretje, I edited my code and got rid of the interrupt part. I just have it looping checking the status of the two sensors. If it is has changed from last cycle, then I am counting it as flowing. It works perfectly well for me and I have some calculations going on the number of pulses to figure gallon flow. It will work out perfectly for me to determine, that, "yes, I have flow at this time" and "total flow through the meter". I appreciate the comments!

I still have some thinking to do to learn more about the interrupt handling....

Rob