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