C 如何检测在Arduino中按下按钮的时间?
如何在Arduino中检测按钮按下/释放的时间,然后打印一些自定义输出?Arduino只能检测按钮的状态(按下或未按下) 您可以使用一个定时器变量(基于他们的文档)来保存按下按钮或释放按钮时的准确时间,这样您就可以检查两个变量之间的差异,以计算保持或空闲的时间 代码应该如下所示:C 如何检测在Arduino中按下按钮的时间?,c,arduino,atmega,C,Arduino,Atmega,如何在Arduino中检测按钮按下/释放的时间,然后打印一些自定义输出?Arduino只能检测按钮的状态(按下或未按下) 您可以使用一个定时器变量(基于他们的文档)来保存按下按钮或释放按钮时的准确时间,这样您就可以检查两个变量之间的差异,以计算保持或空闲的时间 代码应该如下所示: void updateCounter() { // the button is still pressed if (buttonState == HIGH) { holdTime = millis(
void updateCounter() {
// the button is still pressed
if (buttonState == HIGH) {
holdTime = millis() - startPressed;
if (holdTime >= 1000) {
Serial.println("Button is hold for more than a second");
}
// the button is still released
} else {
idleTime = millis() - endPressed;
if (idleTime >= 1000) {
Serial.println("Button is released for more than a second");
}
}
}
const int buttonPin=2;
int buttonState=0;//按钮的当前状态
int lastButtonState=0;//按钮的上一个状态
int startPressed=0;//按下按钮的那一刻
int endPressed=0;//按钮一松开
int holdTime=0;//按钮按下了多长时间
int idleTime=0;//按钮闲置了多长时间
无效设置(){
pinMode(buttonPin,INPUT);//将按钮pin初始化为输入
Serial.begin(9600);//初始化串行通信
}
void循环(){
buttonState=digitalRead(buttonPin);//读取按钮输入
如果(buttonState!=lastButtonState){//按钮状态已更改
不动产();
}
lastButtonState=buttonState;//为下一个循环保存状态
}
无效遗产(){
//按钮刚刚按下
如果(按钮状态==高){
startPressed=millis();
空闲时间=开始加压-结束加压;
如果(空闲时间>=500&&idleTime<1000){
Serial.println(“按钮闲置了半秒钟”);
}
如果(空闲时间>=1000){
Serial.println(“按钮闲置一秒钟或更长时间”);
}
//按钮刚刚松开
}否则{
endPressed=millis();
保持时间=结束按下-开始按下;
如果(保持时间>=500&&保持时间<1000){
Serial.println(“按钮保持了半秒钟”);
}
如果(保持时间>=1000){
Serial.println(“按钮被保持一秒钟或更长时间”);
}
}
}
但是,如果您想在按钮仍然按下时触发事件(或者您想在某些显示中增加计数器),您仍然可以进行相同的计算
将循环函数中的条件更改为:
void updateCounter() {
// the button is still pressed
if (buttonState == HIGH) {
holdTime = millis() - startPressed;
if (holdTime >= 1000) {
Serial.println("Button is hold for more than a second");
}
// the button is still released
} else {
idleTime = millis() - endPressed;
if (idleTime >= 1000) {
Serial.println("Button is released for more than a second");
}
}
}
if(buttonState!=lastButtonState){
UpdateEstate();//按钮状态已更改。它只运行一次。
}否则{
updateCounter();//按钮状态未更改。它在循环中运行。
}
然后像这样实现您的新功能:
void updateCounter() {
// the button is still pressed
if (buttonState == HIGH) {
holdTime = millis() - startPressed;
if (holdTime >= 1000) {
Serial.println("Button is hold for more than a second");
}
// the button is still released
} else {
idleTime = millis() - endPressed;
if (idleTime >= 1000) {
Serial.println("Button is released for more than a second");
}
}
}
出于您的兴趣,这里有一个代码示例,它使用2个数组来存储给定给相应输入按钮的arduino引脚的按钮状态。在循环过程中,您可以使用想要的重复项进行简单检查:
if(button_down(But1_pin, BTN_LOOP_DELAY_MS))
{
// code here repeated if the button is either clicked or maintained
}
button\u down()
还将第一次重复延迟DELAY\u WAIT\u BEFORE\u repeat
ms
下面是完整的测试示例:
#define BTN_LOOP_DELAY_MS 100
#define DELAY_WAIT_BEFORE_REPEAT 500
#define NB_MAX_PIN_INPUT 13
#define But1_pin 7
#define But2_pin 6
// array to check status change
bool prev_button[NB_MAX_PIN_INPUT];
unsigned long button_last_down[NB_MAX_PIN_INPUT];
// macro : our read init with prev_button storage
#define READ_INIT_BUTTON(pin) \
do{ \
pinMode(pin, INPUT); \
prev_button[pin] = digitalRead(pin); \
} while(false)
// function at the end of the code
bool button_down(byte pin_num, unsigned int delay_repeated);
void setup() {
READ_INIT_BUTTON(But1_pin);
READ_INIT_BUTTON(But2_pin);
Serial.begin(115200);
}
void loop() {
if(button_down(But1_pin, BTN_LOOP_DELAY_MS))
{
Serial.print("new inpulse");
Serial.print(millis());
Serial.println();
}
if(button_down(But2_pin, BTN_LOOP_DELAY_MS))
{
Serial.println("button2");
}
}
bool button_down(byte pin_num, unsigned int delay_repeated)
{
bool b = digitalRead(pin_num);
bool r = false;
unsigned long currentMillis = millis();
if(prev_button[pin_num] != HIGH && b == HIGH)
{
r = true;
button_last_down[pin_num] = currentMillis + DELAY_WAIT_BEFORE_REPEAT;
}
else if(b == HIGH
&& prev_button[pin_num] == HIGH
&& currentMillis > button_last_down[pin_num]
&& currentMillis - button_last_down[pin_num] > delay_repeated
)
{
// save the last time we give a button impusle at true
button_last_down[pin_num] = currentMillis;
r = true;
}
// store button state, if changed
if(prev_button[pin_num] != b)
{
prev_button[pin_num] = b;
}
return r;
}