如果按钮在Arduino Uno中保持不变,则在1分钟后执行一个功能

时间:2022-05-08 19:34:39

I'm doing a calculator with Arduino Uno. I'm using a 4x4 keypad, and 16x2 LCD for display. Here's my question: If the buttons on keypad stays untouched for 1 min, the program will execute a function. How can I do this?

我正在用Arduino Uno做计算器。我正在使用4x4键盘和16x2 LCD进行显示。这是我的问题:如果键盘上的按钮保持不动1分钟,程序将执行一个功能。我怎样才能做到这一点?

1 个解决方案

#1


You can use millis() to measure the time and a simple flag boolean with digitalRead() to determine wheter a pushed button is released. It should be something like that (not tested yet):

您可以使用millis()来测量时间,使用一个简单的标志布尔值和digitalRead()来确定释放按钮的时间。它应该是那样的(尚未测试):

unsigned long previousMillis = 0;
long interval = 60000;
int buttonPin = 4;
boolean buttonReleased=false;

void setup(){
}

void loop(){
  unsigned long currentMillis = millis();

  // edit, thanks to frarugi87
  if (digitalRead(buttonPin) == LOW){
    buttonReleased = true; 
    previousMillis = currentMillis; 
  } 

  if((currentMillis - previousMillis > interval)&&buttonReleased ) {
    previousMillis = currentMillis;  
    buttonReleased =false;
    // execute a function
  }
}

EDIT: Correct the keypad condition.

编辑:纠正键盘状况。

#1


You can use millis() to measure the time and a simple flag boolean with digitalRead() to determine wheter a pushed button is released. It should be something like that (not tested yet):

您可以使用millis()来测量时间,使用一个简单的标志布尔值和digitalRead()来确定释放按钮的时间。它应该是那样的(尚未测试):

unsigned long previousMillis = 0;
long interval = 60000;
int buttonPin = 4;
boolean buttonReleased=false;

void setup(){
}

void loop(){
  unsigned long currentMillis = millis();

  // edit, thanks to frarugi87
  if (digitalRead(buttonPin) == LOW){
    buttonReleased = true; 
    previousMillis = currentMillis; 
  } 

  if((currentMillis - previousMillis > interval)&&buttonReleased ) {
    previousMillis = currentMillis;  
    buttonReleased =false;
    // execute a function
  }
}

EDIT: Correct the keypad condition.

编辑:纠正键盘状况。