苦苦挣扎的按钮编码 [英] Struggling coding with buttons

查看:105
本文介绍了苦苦挣扎的按钮编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在为一个问题而苦苦挣扎.

问题是我无法找到一种方法来使一个按钮在8x8显示器上触发一个序列10秒钟然后熄灭,并且还没有另一个按钮使另一个序列在10秒内触发另一个序列然后熄灭,依此类推./p>

如果有人可以帮助我,请回答,将不胜感激:)

这是代码:

    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);
  }
}

这是我从老师那里得到的帮助表,但我无法理解:

帮助表

感谢马特

解决方案

问题是delay(50);.在这50毫秒内,arduino不会检查输入引脚,这很可能是在您按下按钮时出现的.您可以在按下按钮时附加中断功能( https://www.arduino.cc/reference/zh-CN/language/functions/external-interrupts/attachinterrupt/).或者,您可以尝试像这样的循环延迟:

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);.

Ive been really struggling recently with one problem.

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 :)

Here is the CODE:

    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:

Help sheet

Thanks Matt

解决方案

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

From the code you posted it appears you don't need delay(50); at all.

这篇关于苦苦挣扎的按钮编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆