检测Arduino中按下按钮的时间 5S之后输出一个字符串

该博客主要围绕Arduino展开,讲述了检测按钮按下时间的方法,当按钮按下时长达到5秒后,会输出一个字符串,涉及Arduino在时间检测及输出控制方面的应用。
//NodeMcu的管脚定义
#define   D0  16
#define   D1  5
#define   D2  4
#define   D3  0
#define   D4  2//板载Led灯
#define   D5  14
#define   D6  12
#define   D7  13
#define   D8  15
int timeReleased = 0;    
int startPressed = 0;    
void setup() {
  pinMode(D4, INPUT_PULLUP);
  Serial.begin(9600);
}
//此数字用于返回Arduino板开始运行当前程序时的毫秒数。这个数字在大约50天后重叠,即回到零。
void loop() {
  // read the pushbutton input pin:
  //buttonState = digitalRead(buttonPin);
  //按钮按下
  //Serial.println(buttonState);
  if (digitalRead(buttonPin) == false) {
    timeReleased = millis() - startPressed;
    if (timeReleased >= 5000) {
      Serial.println("Button idle for 5 second or more");
    }
  }
  else {
    startPressed = millis();
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值