用按钮苦苦编码 [英] Struggling coding with buttons
问题描述
我最近一直在为一个问题而苦苦挣扎.
Ive been really struggling recently with one problem.
问题是我无法找到一种方法让一个按钮在 8x8 显示器上触发一个序列 10 秒然后熄灭,并且还有另一个按钮可以创建不同的序列 10 秒然后熄灭等等.
The problem is that I cannot find a way to make one button trigger one sequence on an 8x8 display for 10 seconds and go off AND also have another button to make a different sequence for 10 seconds then go off and so on.
如果有人可以帮助我,请回复,将不胜感激:)
If anyone can help me please respond, it would be much appreciated :)
这是代码:
const int buttonPin = 3
int button = 0;
long now = 0;
setup(){
pinMode(buttonPin, INPUT);
}
loop(){
button = digitalRead(buttonPin);
if(button==HIGH){
lc.setRow(0, 0, B00000000);
lc.setRow(0, 1, B00011000);
lc.setRow(0, 2, B00100100);
lc.setRow(0, 3, B01011010);
lc.setRow(0, 4, B10011001);
lc.setRow(0, 5, B10000001);
lc.setRow(0, 6, B11111111);
lc.setRow(0, 7, B00000000);
delay(110);
now = millis();
}
delay(50);
if(millis()>now + 10000){
lc.setRow(0, 0, B00000000);
lc.setRow(0, 1, B00000000);
lc.setRow(0, 2, B00100100);
lc.setRow(0, 3, B01011010);
lc.setRow(0, 4, B10011001);
lc.setRow(0, 5, B10000001);
lc.setRow(0, 6, B11111111);
lc.setRow(0, 7, B00000000);
delay(110);
}
}
这是我从导师那里得到的帮助表,但我无法理解:
Here is the help sheet i got from my tutor but i cant get my head around it:
谢谢马特
推荐答案
问题是delay(50);
.在这 50 毫秒内,arduino 不会检查输入引脚,而且很可能是在您按下按钮时.您可以在按下按钮时附加中断功能(https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/).或者您可以尝试像这样循环延迟:
The problem is delay(50);
. The arduino does not check the input pins during these 50 milliseconds, and it's likely when you are pressing a button. You can either attach an interrupt function when the button is pressed (https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/). Or you could try a looped delay like so:
unsigned int startTime = millis();
unsigned int currentTime = startTime;
while(currentTime - startTime >= 50) {
// check button state
currentTime = millis();
}
https://www.arduino.cc/en/Tutorial/StateChangeDetection
从您发布的代码看来,您根本不需要 delay(50);
.
From the code you posted it appears you don't need delay(50);
at all.
这篇关于用按钮苦苦编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!