I'm new to Arduino and need assistance from knowledgeable people. I'm having a problem with my code, which allows the user to increase or decrease the number by pressing buttons. From time to time, the number does not change by one, but by two or three. This occurs on a sporadic basis. This is known as "bouncing." I googled "attachInterrupt debounce" in vain, but I couldn't find a suitable solution. My code is here.
int digit_state = 0;
void increment(void){
digit_state = digit_state + 1;
}
void decrement(void){
digit_state = digit_state - 1;
}
String user_input = "";
String password = "3452";
int enterbutton = 11;
int increment_button = 2;
int decrement_button = 3;
int counter = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(enterbutton,INPUT);
attachInterrupt(digitalPinToInterrupt(increment_button),increment,FALLING);
attachInterrupt(digitalPinToInterrupt(decrement_button),decrement,FALLING);
}
void loop() {
// put your main code here, to run repeatedly:
lastButtonState = reading;
while(counter<=3){
delay(500);
Serial.println(digit_state);
if(digitalRead(enterbutton)==LOW){
user_input = user_input + digit_state;
Serial.println("Saved input so far is "+user_input);
if(user_input == password){
Serial.println("検証できた");
}
delay(500);
digit_state = 0;
counter++;
}
}
}
As the user presses, I need to stop it from bouncing and get an accurate password all the time.