Program for three water flow sensors

Please share me the codes. I want to use three flow sensors on the Arduino Leonardo. The codes below are my reference but i don’t know how to use it if I would need three sensors. Please help.

volatile int NbTopsFan;                       // measuring the rising edges of the signal
int Calc;
int hallsensor = 2;                           // The pin location of the sensor
 
 
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
}
 
 
void setup()
{
    pinMode(hallsensor, INPUT);               // initializes digital pin 2 as an input
    Serial.begin(9600);                       // This is the setup function where the serial port is initialised,
    attachInterrupt(0, rpm, RISING);          // and the interrupt is attached
}
 
 
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.5);            // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour
 
    Serial.print (Calc, DEC);                 // Prints the number calculated above
 
    Serial.print (" L/hour\r\n");             // Prints "L/hour" and returns a  new line
}

What is the maximum pulse rate from a single sensor?

AWOL:
What is the maximum pulse rate from a single sensor?

450 pulses per liter. I need the program

And the flow rate is is . . ? (i.e. How many firkins per fortnight?)

Does the code you initially posted work correctly with one of your flow sensors?

pdca16:
Please share me the codes. I want to use three flow sensors on the Arduino Leonardo. The codes below are my reference but i don’t know how to use it if I would need three sensors. Please help.

I don’t think that your code will even work with one sensor on a LEONARDO, as you are attaching interrupt-0 to pin-2, which is the assignment for UNO boards.

I tried to fix your code and extended it to use three sensors at the same time.

Untested code for LEONARDO boards, if you want to give it a try:

// Three flow sensors with LEONARDO board only (cannot use this code with UNO)
byte hallsensorPins[] = {3,2,7};             // pin locations for three sensors on LEONARDO
byte hallsensorInt[]  = {0,1,4};             // interrupt numbers assigned to the pins

#define NUMSENSORS (sizeof(hallsensorPins))  // number of sensors

volatile uint16_t count_ISR[NUMSENSORS];     // counters to be used in interrupt service routines
 
 
void hallSensor0()  // ISR for hallsensorPins[0]
{
  count_ISR[0]++; 
}

void hallSensor1()  // ISR for hallsensorPins[1]
{
  count_ISR[1]++; 
}

void hallSensor2()  // ISR for hallsensorPins[2]
{
  count_ISR[2]++; 
}

 
void setup()
{
  Serial.begin(9600);                     // This is the setup function where the serial port is initialised,
  pinMode(hallsensorPins[0], INPUT);   // initializes digital pins as INPUT
  attachInterrupt(hallsensorInt[0], hallSensor0, RISING);  // attach interrupt to sensor
  pinMode(hallsensorPins[1], INPUT);   // initializes digital pins as INPUT
  attachInterrupt(hallsensorInt[1], hallSensor1, RISING);  // attach interrupt to sensor
  pinMode(hallsensorPins[2], INPUT);   // initializes digital pins as INPUT
  attachInterrupt(hallsensorInt[2], hallSensor2, RISING);  // attach interrupt to sensor
}
 
unsigned long lastTime; 
void loop ()
{
  uint16_t count[NUMSENSORS];    // counters that are shadowing the interrupt counters
  if (millis()-lastTime>=1000)
  {
    lastTime+=1000;
    Serial.print("Flow rates: ");
    for (int i=0; i<NUMSENSORS; i++)
    {
      noInterrupts();
      count[i]=count_ISR[i];
      count_ISR[i]=0;
      interrupts();
      int Calc = (count[i] * 60 / 4.5);            // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour
      Serial.print (Calc, DEC);                 // Prints the number calculated above
      Serial.print('\t');
    } 
    Serial.println (" L/hour");             // Prints "L/hour" and returns a  new line
  }
}

jurs:
I don’t think that your code will even work with one sensor on a LEONARDO, as you are attaching interrupt-0 to pin-2, which is the assignment for UNO boards.

I tried to fix your code and extended it to use three sensors at the same time.

Untested code for LEONARDO boards, if you want to give it a try:

// Three flow sensors with LEONARDO board only (cannot use this code with UNO)

byte hallsensorPins = {3,2,7};            // pin locations for three sensors on LEONARDO
byte hallsensorInt  = {0,1,4};            // interrupt numbers assigned to the pins

#define NUMSENSORS (sizeof(hallsensorPins))  // number of sensors

volatile uint16_t count_ISR[NUMSENSORS];    // counters to be used in interrupt service routines

void hallSensor0()  // ISR for hallsensorPins[0]
{
  count_ISR[0]++;
}

void hallSensor1()  // ISR for hallsensorPins[1]
{
  count_ISR[1]++;
}

void hallSensor2()  // ISR for hallsensorPins[2]
{
  count_ISR[2]++;
}

void setup()
{
  Serial.begin(9600);                    // This is the setup function where the serial port is initialised,
  pinMode(hallsensorPins[0], INPUT);  // initializes digital pins as INPUT
  attachInterrupt(hallsensorInt[0], hallSensor0, RISING);  // attach interrupt to sensor
  pinMode(hallsensorPins[1], INPUT);  // initializes digital pins as INPUT
  attachInterrupt(hallsensorInt[1], hallSensor1, RISING);  // attach interrupt to sensor
  pinMode(hallsensorPins[2], INPUT);  // initializes digital pins as INPUT
  attachInterrupt(hallsensorInt[2], hallSensor2, RISING);  // attach interrupt to sensor
}

unsigned long lastTime;
void loop ()
{
  uint16_t count[NUMSENSORS];    // counters that are shadowing the interrupt counters
  if (millis()-lastTime>=1000)
  {
    lastTime+=1000;
    Serial.print(“Flow rates: “);
    for (int i=0; i<NUMSENSORS; i++)
    {
      noInterrupts();
      count[i]=count_ISR[i];
      count_ISR[i]=0;
      interrupts();
      int Calc = (count[i] * 60 / 4.5);            // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour
      Serial.print (Calc, DEC);                // Prints the number calculated above
      Serial.print(’\t’);
    }
    Serial.println (” L/hour”);            // Prints “L/hour” and returns a  new line
  }
}

thanks, btw I modified it already to its corresponding attach interrupt pin. I was just saying that that was my reference. Thanks :slight_smile:

pdca16:
thanks, btw I modified it already to its corresponding attach interrupt pin. I was just saying that that was my reference. Thanks :slight_smile:

BTW: You could easily double the resolution of your flow sensors if you'd change the interrupt mode from RISING to CHANGE and adjust the calculation to:

int Calc = (count[i] * 30 / 4.5);