I've read that the delay function will not run within an interrupt loop. In my sketch it appears to me that the interrupt is closed prior to the delay function yet the delays are not taking place. How could I edit my sketch to incorporate the delay (or its intended purpose- see sketch comments) and not disturb the interrupt's usefulness? Thank you.
// Disable a pressure switch controlled water pump to ignore small (ie: slow leak) flow rates. Results in energy savings while accepting small water losses.
// Code adapted from below referenced and modified by Christopher Norkus
// reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com
volatile int NbTopsFan; //measuring the rising edges of the signal
float Calc;
int inputPin = 2; //The pin location of the sensor
int outPin = 4; //The pin connected to the pump contactor coil. The coil is wired in series with the pressure switch.
void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal
}
// The setup() method runs once, when the sketch starts
void setup() //
{
pinMode(inputPin, INPUT); //initializes digital pin 2 as an input
digitalWrite(inputPin, HIGH); //enables internal pull up resistor
pinMode(outPin, OUTPUT); //initializes digital pin 4 as output
Serial.begin(9600); //This is the setup function where the serial port is initialized,
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop ()
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = (NbTopsFan * 60 / 4.8 * 0.2642); //Pulse frequency (1/min) * 60(min/hr) / 4.8Q, = flow rate (L/hr) * 0.2642 (Gal/L)
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print (" gal/hr\r\n"); //Prints "gal/hour" and returns a new line
if (Calc <30 ) //at 20 psi full demand flow from pressure tank is measured at 41 gph. If flow < this keep pump off. Experiments show that Calc 30 is a good value.
{
digitalWrite(outPin, LOW); //This allows the pump contactor to stay de-energized at low (ie: slow leak) flow rates and energized with actual water demand
delay (10000); //The delay allows for short pulses of demand (ie: car wash nozzle) to be ignored keeping the pump from surging repeatedly.
}
else if (Calc >= 30)
{
digitalWrite(outPin, HIGH); //This energizes the coil, closes the contacts, and passes control to the pressure switch while the high flow condition exists.
delay (10000); //maybe redundant but intended to allow the pump to run for 10 seconds beyond the demand signal and allowing tank to build reserve pressure.
}
}