我想要完成的是在我用手指按下按钮后调用一次按钮。有时它会起作用,但也有一些时候它不起作用。假设我需要从菜单中进行选择。有时,当我按下或向上按钮时,它会完美移动,但有时,只需按一下它就会移动两次。我想解决这个问题。
全球某处:
int debounceDelay = 50;
循环内的代码
a3StateDownButton = digitalRead(A3);
if (a3StateDownButton != a3DownButtonLastState) {
a3DownButtonLastDebounceTime = millis();
}
if ((millis() - a3DownButtonLastDebounceTime) > debounceDelay) {
if (a3StateDownButton != currenta3ButtonState) {
currenta3ButtonState = a3StateDownButton;
if (currenta3ButtonState == HIGH) {
isDownButtonPressed = true;
// do what ever you need to do when button is high
} else if (currenta3ButtonState == LOW) {
isDownButtonPressed = false;
}
}
}
a3DownButtonLastState = a3StateDownButton;
我只有一个电阻连接到其中一个引脚,但我忘记了我输入的值,很可能是 2.2k。
再说一遍,有时它很好,但并不总是完美的。我还认为使用 的值debounceDelay
可能会影响我的菜单,我记得确实如此。值增加时响应变慢。我认为这被称为软件去抖动。也许我可以添加一些东西来使它成为硬件去抖动。