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