Use multiple interrupt for flow sensor

Hi. I'm new on Arduino coding and want to use the board for monitoring the flow rate with Mega board. I use following code to read the flow sensor and convert in to meaningful result. But the problem is this code works only for 1 sensor. I was wondering is there any way to use interrupt for reading 4 sensor at the same time? I mean I have four path on my setup and need to monitor the flow rate all of them and the same time. How can I use interrupt for calculate 4 flow rates simultaneously?

volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
flow_frequency++;
}
void setup()
{

pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
Serial.begin(9600);
attachInterrupt(0, flow, RISING); // Setup Interrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 23Q, Q is flow rate in L/min.
l_hour = ((flow_frequency * 60 / 23)*1.1); // (Pulse frequency x 60 min) / 23Q = flowrate in L/hour
flow_frequency = 0; // Reset Counter
Serial.print(l_hour, DEC); // Print litres/hour
Serial.println(" L/hour");
}
}

By using four interrupts.

You should note that flow_frequency should be read and zeroed in a critical section, with interrupts disabled.

Please remember to use code tags when posting code

Before any further, please tell us how many hindred times per second the flow sensor creates an interrupt? This is the only reason for using an interrupt.
I am guessing you can watch for a low to high change on any pin connected to your flow sensors by reading the pin in the loop() code.

How long is each interrupting puls?
I think about using 4 diodes that triggers the interrupt whenever there is a pulse. Inside the ISR, check which sensor was pulsing and update the data for that sensor.

Why make things harder?

It's a Mega.

Even using pin change interrupts on a Uno would be simpler.

My limited knowledge.... Got the idea that only 2 pins could trigger interrupts, pin 2 and 3.........
Checking the reference manual now....

Using interrupts for a flow sensor that might be giving less than 10 pulses per second is like using an expensive stopwatch for a turtle-race. Polling (pin change) would be much easier.
Leo..

1 Like

Haha. You gave me the laugh of the day.
True, don't use more muscles than really needed.

My neighbor had a pet turtle, she went away to college and her parents took over care for the pet. I would see them out in the backyard walking the turtle (really). One day there were out in the yard sitting on lawn chairs with the turtle nearby. The lost track of time a little and the turtle was off !! They never found the turtle. So maybe when we are not looking turtles are faster than we think.

You should find that Mega has six. Unfortunately I recall, the extras share with serial and I2C, but this may not be a problem for OP. I'm sure multiple flow meters has has been addressed before.

1 Like

VT100 or later? :rofl:

1 Like

I hate auto correct....

For a minute there I was sad about the terminal pet.

a7

Hi
Please read : How to get the best out of this forum
and redo your initial post putting the code between the </> tags.
ArduinoForum

Test the code I posted. (I only tested by compiling and not loading in a MEGA or running ).

volatile int flow_frequency2, flow_frequency3, flow_frequency18, flow_frequency19 ; // Measures flow sensor pulses
unsigned int l_hour2, l_hour3, l_hour18, l_hour19; // Calculated litres/hour
const byte flowsensor2  =  2;  // Sensor Input 1
const byte flowsensor3  =  3;  // Sensor Input 2
const byte flowsensor18 = 18;  // Sensor Input 3
const byte flowsensor19 = 19;  // Sensor Input 4
unsigned long currentTime;
unsigned long cloopTime;
//------------------------------------------------------------------
void flow2 () // Interrupt function
{
  flow_frequency2++;
}
//------------------------------------------------------------------
void flow3 () // Interrupt function
{
  flow_frequency3++;
}
//------------------------------------------------------------------
void flow18 () // Interrupt function
{
  flow_frequency18++;
}
//------------------------------------------------------------------
void flow19 () // Interrupt function
{
  flow_frequency19++;
}
//------------------------------------------------------------------
void setup()
{
  pinMode(flowsensor2,  INPUT_PULLUP);  // Optional Internal Pull-Up
  pinMode(flowsensor3,  INPUT_PULLUP);  // Optional Internal Pull-Up
  pinMode(flowsensor18, INPUT_PULLUP);  // Optional Internal Pull-Up
  pinMode(flowsensor19, INPUT_PULLUP);  // Optional Internal Pull-Up

  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(flowsensor2),  flow2,  RISING); // Setup Interrupt
  attachInterrupt(digitalPinToInterrupt(flowsensor3),  flow3,  RISING); // Setup Interrupt
  attachInterrupt(digitalPinToInterrupt(flowsensor18), flow18, RISING); // Setup Interrupt
  attachInterrupt(digitalPinToInterrupt(flowsensor19), flow19, RISING); // Setup Interrupt
  currentTime = millis();
  cloopTime = currentTime;
}
//------------------------------------------------------------------
void loop ()
{
  currentTime = millis();
  // Every second, calculate and print litres/hour
  if (currentTime >= (cloopTime + 1000))
  {
    cloopTime = currentTime; // Updates cloopTime
    // Pulse frequency (Hz) = 23Q, Q is flow rate in L/min.
   
    l_hour2 = ((flow_frequency2 * 60 / 23) * 1.1); // (Pulse frequency x 60 min) / 23Q = flowrate in L/hour
    flow_frequency2 = 0; // Reset Counter
    Serial.print(l_hour2, DEC); // Print litres/hour
    Serial.println(" L/hour 2 ");

    l_hour3 = ((flow_frequency3 * 60 / 23) * 1.1); // (Pulse frequency x 60 min) / 23Q = flowrate in L/hour
    flow_frequency3 = 0; // Reset Counter
    Serial.print(l_hour3, DEC); // Print litres/hour
    Serial.println(" L/hour 3 ");

    l_hour18 = ((flow_frequency18 * 60 / 23) * 1.1); // (Pulse frequency x 60 min) / 23Q = flowrate in L/hour
    flow_frequency18 = 0; // Reset Counter
    Serial.print(l_hour18, DEC); // Print litres/hour
    Serial.println(" L/hour 18 ");

    l_hour19 = ((flow_frequency19 * 60 / 23) * 1.1); // (Pulse frequency x 60 min) / 23Q = flowrate in L/hour
    flow_frequency19 = 0; // Reset Counter
    Serial.print(l_hour19, DEC); // Print litres/hour
    Serial.println(" L/hour 19 ");
  }
}

As noted above, the variables incremented in the ISRs should be accessed in loop in a critical section, since an int access is not atomic on the Mega

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.