arduino - 延迟防止键盘输入直到它结束
问题描述
我正在尝试使用 4*4 键盘设置闪烁 LED 的延迟,但是当延迟很大时,您必须等待它结束,以便您可以使用键盘输入另一个数字。那么如何在延迟开启时获得键盘的输入?
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key != 'A' && key != 'B' && key != 'C' && key != 'D' && key != '*' && key != '#') {
Serial.print(key - 48);
value = value + key;
num = value.toInt();
}
}
if (key == 'D'){
wait = num;
value = "";
}
digitalWrite(led, 1);
delay(wait);
digitalWrite(led, 0);
delay(wait);
}
解决方案
您可以使用该millis()
函数创建无延迟闪烁功能,并在按键后调用它。此外,这里有一个很好的例子。
所以你可以用这样的东西重写你的代码:
int wait;
int ledState = LOW; // ledState used to set the LED
unsigned long currentMillis, previousMillis;
void blink_without_delay()
{
currentMillis = millis();
if (currentMillis - previousMillis >= wait)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
void setup()
{
// Do your setup in here
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
if (key != 'A' && key != 'B' && key != 'C' && key != 'D' && key != '*' && key != '#')
{
Serial.print(key - 48);
value = value + key;
num = value.toInt();
}
}
if (key == 'D')
{
wait = num;
value = "";
}
blink_without_delay();
}
推荐阅读
- xamarin - 由字符串组成的 ObservableCollection 不更新
- php - 我需要将来自多动态选择的信息保存到数据库
- javascript - 用 Flow 做“导入类型”而不是“导入”有什么意义吗?
- python - 错误:对于 pygame.event.get() 中的事件:pygame.error:视频系统未初始化
- php - 使用 Guzzle 和 SilverStripe 将文件上传到外部服务
- css - 在 Aurelia 中使用两个 style.bind
- javascript - 通过 XSL 变量传递到内联 JavaScript 函数
- c# - 如何将一些信息转移给其他人?
- ios - 动态调整 Tableview 高度和单元格?
- spring - Spring Websocket 400 错误:由于升级标头无效,握手失败:null