由于millis()函数,我有一个程序每100毫秒更新一次。该程序正在等待一个按钮被按下,当它发生某些事情时。现在,我想知道按钮何时被释放,等待大约半秒钟,然后将连续旋转伺服移动大约 45 度。
我怎么做?附上我的部分代码。
#define UPDATE_MS (100)
#define BTN_PIN (5)
bool btn_state = 1;
bool prev_btn_state = 1;
void loop () {
if (millis() - previous_millis >= UPDATE_MS) {
previous_millis = millis();
btn_state = digitalRead(BTN_PIN);
if (!btn_state) {
//It does somethings - doesn't matter right now...
prev_btn_state = 0;
}
}
if (btn_state != prev_btn_state) {
magazine_servo.write(45);
}
prev_btn_state = btn_state;
}