# Need help please for faster Flowsensors  loop

**URL:** <https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596>\
**Category:** Programming\
**Created:** [July 24, 2015, 12:25am UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596 "2015-07-24T00:25:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![flippomatik](https://avatars.discourse-cdn.com/v4/letter/f/b487fb/32.png) [@flippomatik](https://forum.arduino.cc/u/flippomatik)\
**Post date:** [July 24, 2015, 12:25am UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/1 "2015-07-24T00:25:06Z")

</div>

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);

}

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [July 24, 2015, 12:39am UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/2 "2015-07-24T00:39:42Z")

</div>

pulseIn() doesn't return a Boolean.

> **[pulseIn() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![flippomatik](https://avatars.discourse-cdn.com/v4/letter/f/b487fb/32.png) [@flippomatik](https://forum.arduino.cc/u/flippomatik)\
**Post date:** [July 24, 2015, 1:28am UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/3 "2015-07-24T01:28:23Z")

</div>

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);

}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 24, 2015, 2:35am UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/4 "2015-07-24T02:35:02Z")

</div>

```cpp
  Imp1 = Imp1++;
  Imp2 = Imp2++;

```

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

Use either:

```cpp
  Imp1++;
   Imp2++;

```

or

```cpp
  Imp1 = Imp1 + 1;
   Imp2 = Imp2 + 1;

```

```cpp
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.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [July 24, 2015, 8:10am UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/5 "2015-07-24T08:10:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flippomatik](https://avatars.discourse-cdn.com/v4/letter/f/b487fb/32.png) [@flippomatik](https://forum.arduino.cc/u/flippomatik)\
**Post date:** [July 24, 2015, 11:08am UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/6 "2015-07-24T11:08:49Z")

</div>

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](http://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](http://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](https://forum.arduino.cc/uploads/short-url/dm1BHI8KCN0insRjSt7mSWgR5Vg.ino) (18.6 KB)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 24, 2015, 1:10pm UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/7 "2015-07-24T13:10:02Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [July 24, 2015, 2:33pm UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/8 "2015-07-24T14:33:57Z")

</div>

I agree with @PaulS's assessment.

If you are new to interrupts I suggest you start with reading a single flow meter using [attachInterrupt()](https://www.arduino.cc/en/Reference/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](http://www.gammon.com.au/forum/?id=11488)

...R

---

<div class="post-metadata">

**Author:** ![flippomatik](https://avatars.discourse-cdn.com/v4/letter/f/b487fb/32.png) [@flippomatik](https://forum.arduino.cc/u/flippomatik)\
**Post date:** [July 24, 2015, 7:07pm UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/9 "2015-07-24T19:07:53Z")

</div>

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

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:24pm UTC](https://forum.arduino.cc/t/need-help-please-for-faster-flowsensors-loop/325596/10 "2021-05-05T21:24:09Z")

</div>

Closed on May 5, 2021, 9:24pm UTC


