Closed fernando-inf closed 3 years ago
Before I was using "while" which works perfect except for interference noises. When I bring my fingers closer, the push button is activated.
int startTime1 = 0;
int bank = 0;
int toogleState = 0;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
}
void loop() {
while (digitalRead(2) == LOW) {
delay(10);
startTime1 = startTime1 + 10;
}
//FUNCTION 1
if (startTime1 >= 500) {
bank++;
Serial.print("Bank:");
Serial.println(bank);
}
//FUNCTION 2
else if (startTime1 >= 5) {
toogleState++;
Serial.print("ToogleSate:");
Serial.println(toogleState);
}
startTime1 = 0;
}
When the state changes, the currentDuration is always 0 since the state changed : one the state changes, currentDuration resets to 0. You probably want previousDuration.
Thank you very much for the prompt response and the suggestion! You were right, I fixed it with .previousDuration ()
. The code is like this and it works perfect:
#include <Bounce2.h>
Bounce2::Button pedal_1 = Bounce2::Button();
int bank = 0;
int toogleState = 0;
void setup() {
Serial.begin(9600);
pedal_1.attach(2, INPUT_PULLUP);
pedal_1.interval(25);
//pedal_1.setPressedState(LOW);
}
void loop() {
pedal_1.update();
if ( pedal_1.changed() ) {
if ( pedal_1.read() == HIGH ) {
//FUNCTION 2
if ( pedal_1.previousDuration() > 500 ) {
toogleState++;
Serial.print("ToogleSate:");
Serial.println(toogleState);
}
//FUNCTION 1
else {
bank++;
Serial.print("Bank:");
Serial.println(bank);
}
}
}
}
You are welcome.
Hi, I have tried using the "
.currentDuration ()
" function from the Bonce2 library, but the best I have achieved is using with ".rose
" and ".isPressed
". However, when you keep the button pressed to go to function 2, function 1 is also activated. In my example, if I want to change only the value of "toogleState
", I cannot do it without also changing the value of "bank". And as you can see I also had to add a Delay in function 2 and a double condition "&& pedal_1.currentDuration ()> 500 && pedal_1.currentDuration () <1000
" to avoid problems.