Need help please for faster Flowsensors loop

Hello, my problem is when the flowmeters don´t send data it takes almost a second to dispay it on the serial monitor and than i don´t get the correct values for the second flowmeter . Does anybody have an answer to my problem?

///////////Here is the code//////////////////////////////////////////////////////////////

long Imp1; long Imp2;

void setup() { Serial.begin(9600); pinMode(2, INPUT);

}

void loop() {

Imp1 = Imp1++; Imp2 = Imp2++;

if (pulseIn(2, HIGH) ) Imp1++;

Serial.println(Imp1);

if (pulseIn(3, HIGH) ) Imp2++;

Serial.println(Imp2);

}

pulseIn() doesn't return a Boolean. https://www.arduino.cc/en/Reference/pulseIn

When I do it this way I get quick readings but no value. (I am totally new to programming)

long Imp1; long Imp2;

void setup() { Serial.begin(9600); pinMode(2, INPUT);

}

void loop() {

Imp1 = Imp1++; Imp2 = Imp2++;

if (pulseIn(2, HIGH,10) ) Imp1++;

Serial.println(Imp1);

if (pulseIn(3, HIGH,10) ) Imp2++;

Serial.println(Imp2);

}

  Imp1 = Imp1++;
  Imp2 = Imp2++;

Absolute nonsense. You have no idea what value will actually be stored in Imp1 or Imp2.

Use either:

  Imp1++;
   Imp2++;

or

  Imp1 = Imp1 + 1;
   Imp2 = Imp2 + 1;
if (pulseIn(2, HIGH,10) ) Imp1++;

This makes no sense, either. The pulseIn() function measures how long it takes for the pin to go HIGH. Using that as a boolean does not make sense.

You probably ought to be using interrupts to measure pulses from the flow sensor.

You need to tell us what you are trying to do because it seems that the method you have chosen is not suitable. But it is impossible to suggest a sensible alternative until you provide a good description of the project.

Post a link to the datasheet for the flowmeter you are using.

What range of flow rates do you want to measure.

...R

Hallo thank you for your help.

I want to have a program with seven of these flowcounters measuring up to 1 Liter each.

www.ebay.de/itm/351333874065?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

and two of these flowcounters measureing up to 200 Liters:

www.ebay.de/itm/141563053195?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

Each flowcounter are connected to a pump that otherwise connected to a relay and triggered by an RTC.

If the flowcounters reached the desired value they have to stop the relay immediately

but now the readings takes to long so the flowcounters don´t start in time and don´t stop in the desired range .

Everything else is working fine.

The code is to long to post it so i attached it.

relay_versuch_2.ino (18.6 KB)

but now the readings takes to long so the flowcounters don´t start in time and don´t stop in the desired range .

You will most likely need to use interrupts to read these sensors. Each time the sensor detects that some volume of liquid has passed, it should tell you, by setting a pin HIGH (or taking it LOW). That should trigger an interrupt. In the interrupt service routine, you can shut the relay off nearly immediately after the correct amount of liquid has moved, based on how many times the ISR is called.

You do NOT want to sit and stare at one sensor at a time waiting for that volume of liquid to pass, so that pulseIn() can detect it. THAT is what you are doing now, so it is no wonder that the code is slow.

The only real challenge is that there is no Arduino that has 7 external hardware interrupts. The good news is that most pins on most Arduinos can be set to use pin change interrupts. The drawback here is that in the ISR, you need to which pin caused the interrupt and react appropriately.

I agree with @PaulS's assessment.

If you are new to interrupts I suggest you start with reading a single flow meter using attachInterrupt()

That way you get a chance to study the process with simple code that we can easily help with. When you have that working you can then consider using multiple interrupts.

Nick Gammon has a good tutorial on interrupts

...R

Ok it seems to be very complicated but i will try it.

Thank you