I've used the following code to read from a hall effect sensor. It works perfectly fine on its own but when I tried to integrate it into my main project, when the function flow_sensor() is called in void loop, the program gets stuck there. I believe it might have to do with the interrupts or delay(1000). I've tried different things with no luck. Hopefully it's an easy fix for any of you and I would really appreciate if anyone can help me. Thanks.
void flow_sensor()
{
count = 0; // Reset the counter so we start counting from 0 again
interrupts(); //Enables interrupts on the Arduino
delay (1000); //Wait 1 second
noInterrupts(); //Disable the interrupts on the Arduino
flowRate = (count * 2.25); //Take counted pulses in the last second and multiply by 2.25mL
Total_mL = (flowRate + Total_mL); //Transfer flowrate value to Total_mL
Total_L = Total_mL / 1000; //divide by 1000 to get into liters
flowRate = flowRate * 60; // mL/minute
flowRate = flowRate / 1000; // liters/minute
Total_G = (Total_mL * 0.000264172052); //convert mL to gallons
}
void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}
I used the following code which I found online. I ran this code on its own and it works. I then put what is in this loop into its own function called void flow_sensor into another sketch which is my main project. My main project sketch is very long and it does not use any other interrupts except this one. When void flow_sensor is called in my main project's sketch loop, it gets stuck in the function. Am I implementing it wrong?
int flowPin = 2; //input pin 2 for interrupt
double flowRate; //rate of water flow
double Total_mL; //total water in Milliliters
double Total_L; //total water in Liters
double Total_G; //total water in Gallons
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
void setup()
{
pinMode(flowPin, INPUT_PULLUP); //sets the pin as an input pullup
attachInterrupt(0, Flow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
Serial.begin(9600);
}
void loop()
{
count = 0; // Reset the counter so we start counting from 0 again
interrupts(); //Enables interrupts on the Arduino
delay (1000); //Wait 1 second
noInterrupts(); //Disable the interrupts on the Arduino
flowRate = (count * 2.25); //Take counted pulses in the last second and multiply by 2.25mL
Total_mL = (flowRate + Total_mL); //Transfer flowrate value to Total_mL
Total_L = Total_mL / 1000; //divide by 1000 to get into liters
flowRate = flowRate * 60; // mL/minute
flowRate = flowRate / 1000; // liters/minute
Total_G = (Total_mL * 0.000264172052); //convert mL to gallons
Serial.print("Flow Rate: ");
Serial.print(flowRate);
Serial.print(" L/M");
Serial.print(" Total ");
Serial.print(Total_mL);
Serial.print(" mL ");
Serial.print(" Total ");
Serial.print(Total_L);
Serial.print(" Liters ");
Serial.print(" Total ");
Serial.print(Total_G);
Serial.println(" Gallons");
}
void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}