Struggling to implement a 'debounce' feature into a counter

Hi,
I am currently working on a piece of code which is designed to register breaks in two infra-red sensors, sensorA and sensorB.
While the actual counting is working fine, implementing a feature similar to a 'debounce' on a button seems to be proving a tad difficult and i would like to request some help.

const int SensorA = A0;
const int SensorB = A1;

int Va = 0;
int Vaa = 0;

int Vb = 0;
int Vbb = 0; 

int countUP = 0;
int countDOWN = 0; 

void setup()  {
  Serial.begin(9600);
}

void loop()  {
 
int stateA = LOW;

if (stateA == LOW)   {
    Va = analogRead(SensorA); 
  while (Va > 900)  { 
        Vb = analogRead(SensorB);
      if (Vb > 900)  {
          stateA = LOW;
          Va = 0; 
          countUP ++;
 
  if (countUP > 0)  {
    Serial.print("Fish detected, upstream: ");
    Serial.println(countUP); 
    
}
}
 }
}
  int stateB = HIGH;
  
 if    (stateB == HIGH)  {
        Vbb = analogRead(SensorB); 
    while (Vbb > 900)  {
      Vaa = analogRead(SensorA);
     if (Vaa > 900) {
         stateB = HIGH;
         Vbb = 0;
         countDOWN ++;
         
     if (countDOWN > 0)  {
       Serial.print("Fish detected, downstream: ");
       Serial.println(countDOWN);
     }
     }
    }
 }
 
}

Thank you

and i would like to request some help.

I'd like you to put every { on a new line, use Tools + Auto Format, and explain what you need help with. There is nothing in that code that implements debouncing, nor is it clear what you want to debounce.

Simple way is to use millis() and not let another reading start until so much time has passed. Check your reading then, and if its still greater than 900, wait another time period before making another reading.

Thanks for the replies,

The optimal feature would be one which doesn't count until the values drop back to zero.
and here's my code properly formatted

const int SensorA = A0;
const int SensorB = A1;

int Va = 0;
int Vaa = 0;

int Vb = 0;
int Vbb = 0; 

int countUP = 0;
int countDOWN = 0; 

void setup()  
{
  Serial.begin(9600);
}

void loop()  
{

  int stateA = LOW;

  if (stateA == LOW)   
  {
    Va = analogRead(SensorA); 
    while (Va > 900)  
    { 
      Vb = analogRead(SensorB);
      if (Vb > 900)  
      {
        stateA = LOW;
        Va = 0; 
        countUP ++;

        if (countUP > 0)  
        {
          Serial.print("Fish detetcted, upstream: ");
          Serial.println(countUP); 

        }
      }
    }
  }
  int stateB = HIGH;

  if    (stateB == HIGH)  
  {
    Vbb = analogRead(SensorB); 
    while (Vbb > 900)  
    {
      Vaa = analogRead(SensorA);
      if (Vaa > 900) 
      {
        stateB = HIGH;
        Vbb = 0;
        countDOWN ++;

        if (countDOWN > 0)  
        {
          Serial.print("Fish detected, downstream: ");
          Serial.println(countDOWN);
        }
      }
    }
  }

}

The optimal feature would be one which doesn't count until the values drop back to zero.

The optimal requirement would be one that stated WHICH values need to drop back to zero.

Either sensor A or sensor B, as the code increments the count after a value has been registered by Sensor X then sensor Y (the sensors are interchangable as the code is designed to check whether the direction the subject is moving in.

So if the subject goes through sensor A then sensor B, the debounce feature would have to be implemented by sensor B and vice versa.

Either sensor A or sensor B

Sensor A and sensor B are sensors connected to the Arduino. They are not values.

SensorA and SensorB in your code are pin numbers. They will NEVER drop back to 0.

If you are trying to refer to Va and Vb, say so.

  int stateA = LOW;

  if (stateA == LOW)

Well, of course it will. Pretty useless to check something you KNOW to be true.

I did mean Va & Vb, and as i am new to arduino, and new to programming.
The stateA = LOW is a constant and i put it in as i found that it worked for me.

But could i get help with the actual problem? The code is in it's early stage (clearly) and i can clean it up later.
How would i implement a feature which can do this for me? i looked at the button debounce example but i couldn't find the place to try and adapt the code into

How would i implement a feature which can do this for me? i looked at the button debounce example but i couldn’t find the place to try and adapt the code into

Debouncing a switch (ignoring change that happen within small intervals of one event) and what you want to do are completely different things.

You need some boolean variables (flags), and to get rid of the while loops. Set a flag to true if Va is above the threshold (fish passing?) and set it to false if it is below the threshold. Set another if Vb is above or below. Doing this is completely independent of what else the sketch is doing.

bool flagA = false;
bool flagB = false;

void loop()
{
    flagA = analogRead(SensorA) > 900;
    flagB = analogRead(SensorB) > 900;

// The rest of the code
}

Then, you use flagA and flagB to determine what is happening.

You can keep previous copies of the flags so you can tell when the sensor reading transitioned to above or below the threshold.

I don’t think that debouncing is what you need. I do think that “state change detection” is what you need. Look at the example with that name.

Ah that does make sense and would clean things up nicely!

I'll try and implement this in a few hours and hopefully it will resolve itself,
if not.. I'll be back..