Using 2 flow meters

Please do.

At least this is wrong:

void loop() {
    //main code to run repeatedly
  countA = 0;    //makes counter start at 0
  countB = 0;




It resets the counters every trip of the loop() function. Control passes through those lines many thousands of times per second, as you have coded a free running loop. Which is good, but has implications.

a7

Should that part go in the void setup () part?

No. Those variables will be zero when they are created.

You need to set them back to zero after you calculate a result. So they are ready to count again. In my solution, we didn't need to do that; the period count is simply the current count minus whatever it was at the start of the period. Unsigned maths is the key here, and operates perfectly for differences that are small compared to the size of the variables holding the values.

Don't forget that any use of those variables outside the ISR must be done with interrupts temporarily turned off.

There are now three ways presented that work. Without analysis I am too lazy don't have time to do just now, it is not clear which succeeds best at counting during a fixed time period.

But a glance suggests other factors will be more important or also negligible.

I have not read the rest of your code, that's just what jumped.

I suggest you try to understand all the code that has been offered here. Reading and asking questions or researching for answers will make things like this seem so easy after having been so hard.

a7

1 Like

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