Only read large water samples using a bubble detector and Millis()

Hi all, first off, just want to clarify that I read through the Millis() pinned forum post!

I have a pump setup that pushes water through some tubes. Connected to one of these tubes is a bubble detector. We are using this to detect when a new water sample comes through. However, whenever I toggle between water and air to separate each water sample, some left over water bubbles are pushed through the tube and our bubble detector is obviously picking these up.


( water sample ) () ( water sample ) <-----tube

^
^
dont want to read that

Thus, I wanted to implement some sort of “debouncing” but for water bubbles. If the bubble detector detects water flow AND two seconds later it still detects water flow THEN we can confirm that this is a new water sample and increment the count. Right now, my code picks up every bubble and still increments even after implementing a millis delay and comparing two readings.

Here’s my code, both reportBD() and debounceBD() are referenced in my loop() so both of these functions are being checked as fast as possible

#include "Arduino.h"
#include "system.h"
#include "Bubble_Detect.h"

uint8_t bubble_count = 0;
bool isBubble = 0;
bool newSample = 0;
unsigned long flowStartMillis = 0;
uint8_t first_comparator = 0;
uint8_t second_comparator = 0;

void debounceBD() {
  if (newSample == 1) {

    if(millis() - flowStartMillis > 2000){
      second_comparator = digitalRead(7);
      if((first_comparator == 1) && (second_comparator == 1)) {
        bubble_count += 1;
        serial_results[0] = bubble_count;
        Serial.print(bubble_count);
        newSample = 0;
      }
      first_comparator = 0;
      second_comparator = 0;
    }
  }
}

void reportBD(){
  
  uint8_t detect = digitalRead(7);
  
  if(detect == 0){
    //if now flow is detected, then this must mean air gap
    isBubble = 1;
  } else if ( detect == 1 && isBubble == 1) {

    //water flow is detected and we were just in an air gap, go to debounceDB()
    newSample = 1;
    first_comparator = digitalRead(7);
    flowStartMillis = millis();
    isBubble = 0;

  } else if ( detect == 1 && isBubble == 0) {
    //flow is still detected
    isBubble = 0;
  }  
}

void initBD(){
  pinMode(7, INPUT); 
}

dont want to read that

You don't have any choice in the matter. The sensor IS going to sense that small air bubble.

What you CAN do is to ignore the data from the sensor, if the "there is a bubble in the way" signal is of short enough duration.

Thus, I wanted to implement some sort of "debouncing" but for water bubbles.

You want to stop the bubbles from bouncing?

What do 0 and 1 mean, as returned by digitalRead() of pin 7? Which value means that there is a bubble in the way?

You need to record when, using millis(), the state of the pin BECOMES "bubble in the way". You need to record when, using millis(), the state of the pin BECOMES "no bubbles in my beer".

When the state becomes "no bubble", you can determine how long the bubble was in the way. That amount of time may, or may not, be significant enough to warrant action.

PaulS: You don't have any choice in the matter. The sensor IS going to sense that small air bubble.

What you CAN do is to ignore the data from the sensor, if the "there is a bubble in the way" signal is of short enough duration. You want to stop the bubbles from bouncing?

What do 0 and 1 mean, as returned by digitalRead() of pin 7? Which value means that there is a bubble in the way?

You need to record when, using millis(), the state of the pin BECOMES "bubble in the way". You need to record when, using millis(), the state of the pin BECOMES "no bubbles in my beer".

When the state becomes "no bubble", you can determine how long the bubble was in the way. That amount of time may, or may not, be significant enough to warrant action.

Hey Paul, sorry wrong phrasing. I know the sensor will detect it, I just dont want to count it on the software side. If the sensor detects water that digitalRead will go HIGH (or 1) and if air is detected then it will go LOW (or 0). What you described is exactly the method that I am trying to accomplish.

I first check to see if there is water flow, if not, then we know we are in an airgap, so isbubble = 1 (isbubble means air bubble). Then it loops through again and this time we detect water! So detect = 1 and we were previously in an air gap so isbubble is also 1, thus we have a new water sample. I take my first comparator sample to detect if water is flowing and it should read HIGH. now since newSample = 1, we jump up to the top function. Run millis() delay for 2 seconds and run another comparator to see if the water is still flowing. Then if both comparators are high (meaning there was water flowing for two seconds) we can confirm that this is a new water sample and not a small blip picked up by the bubble detector and we increment the sample count