Water Flow Sensor Problem

hi everyone,

i have a problem to read 3 different water flow sensors. i did something wrong in my program but i don´t know where is the mistake. may someone can help me

volatile int flow_frequency1;
volatile int flow_frequency2;
volatile int flow_frequency3;
unsigned int l_min1;
unsigned int l_min2;
unsigned int l_min3;
unsigned char flowmeter1 = 2;
unsigned char flowmeter2 = 3;
unsigned char flowmeter3 = 4;
unsigned long currentTime;
unsigned long cloopTime;

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

void flow ()
{
flow_frequency1++;
flow_frequency2++;
flow_frequency3++;
}

void setup()
{
pinMode (flowmeter1, INPUT);
pinMode (flowmeter2, INPUT);
pinMode (flowmeter3, INPUT);
Serial.begin (9600);
attachInterrupt (0, flow, RISING);

sei();
currentTime = millis();
cloopTime = currentTime;

lcd.init();
lcd.backlight();

}

void loop()
{
currentTime = millis();
if (currentTime >= (cloopTime + 500))
{
cloopTime = currentTime;
l_min1 = (flow_frequency1 / 7.5 );
l_min2 = (flow_frequency2 / 7.5 );
l_min3 = (flow_frequency3 / 7.5 );
flow_frequency1 = 0;
flow_frequency2 = 0;
flow_frequency3 = 0;
Serial.print (l_min1, DEC);
Serial.print (" L/min");
Serial.print (l_min2, DEC);
Serial.print (" L/min");
Serial.print (l_min3, DEC);
Serial.print (" L/min");

lcd.setCursor (0,0);
lcd.print (" Jacob GmbH “);
lcd.setCursor (0,1);
lcd.print (” Temp 1: “);
lcd.print (l_min1);
lcd.print (” L/min");
lcd.setCursor (0,2);
lcd.print (" Temp 2: “);
lcd.print (l_min2);
lcd.print (” L/min");
lcd.setCursor (0,3);
lcd.print (" Temp 3: “);
lcd.print (l_min3);
lcd.print (” L/min");
delay (5000);
lcd.clear ();
}

}

only the first sensor is working.
thanks for the help

You only attached an interrupt for the pin for the first sensor but then you try to update values for all 3 in that ISR. It will only trigger the interrupt for the sensor on pin 2

Please use code tags when posting code. Read the forum rules if you don't know what that means.

if (currentTime >= (cloopTime + 500))

That isn't safe across millis rollover. You should always use the form

if(currentTime - cloopTime >= 500)

to guard against rollover.

so you mean it should be like this?

void flow  ()
{
  flow_frequency1++;
    }
void flow2  ()
{
  flow_frequency2++;
    }
void flow3  ()
{
  flow_frequency3++;
    }
   


void setup() 
{
  pinMode (flowmeter1, INPUT);
  pinMode (flowmeter2, INPUT);
  pinMode (flowmeter3, INPUT);
  Serial.begin (9600);
  attachInterrupt (0, flow, RISING);
  attachInterrupt (0, flow2, RISING);
  attachInterrupt (0, flow3, RISING);

im sorry i just started to write programs

Go look at the reference for attachInterrupt. If you try to attach them all to interrupt 0 then only the last one will work.

What type of Arduino do you have? If it is the UNO then you're out of luck as it has only 2 hardware interrupts. You'll have to change over at least one to a pin change interrupt. If you have an UNO then please look that "pin change interrupt" up as well.

yes its an UNO.
i totaly have no idea how i can solve that. I will google the pin change function.

Thanks for your help :slight_smile:

ok i don´t have a glue how it works.

i cant help myself -.-

You didn't try very hard. Spent a whole 19 minutes on it.

There's a section called Gigs and Collaborations where you can pay someone to write code for you if you don't want to put in any work on your own.

Otherwise it might take a few hours at least to go learn how interrupts work.

See here. https://gammon.com.au/interrupts

thank you for the link, i hope it will help me :slight_smile:

Also, when you use a LCD, like you are using, display the constant values ONE time at the beginning. Then check each variable you are displaying to see if is different from the last time you displayed it. If different, display a blank where the variable will go, then display the variable and save the new value for the next display time. Use the LCD cursor control to position the cursor to the beginning of the the display field.

LCD display takes a long time to do.

Paul

When you review the Gammon tutorial and become more familiar with interrupts you may want to use a library for pin change interrupts rather than write your own at the timer level.

I would take a look at Nico Hood's PinChangeInterrupt library available through the library manager. The syntax is analogous to the external interrupts so its easy and familiar.

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
attachPCINT(digitalPinToPCINT(pin), ISR, mode);