我已经在一个问题中询问了这个程序,关于闪烁的LED由一个按钮控制,但现在我想问我的程序是否能够长按5秒然后它会开始闪烁循环,如果长按按钮的时间少于或超过5秒。
int buttonPin = D0;
int ledPin = D1;
int held = 5;
int ledState = LOW;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop()
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonState)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != buttonState)
{
buttonState = reading;
if (buttonState == HIGH && held == 5)
{
BLINK;
if {
A;
}
else {
B;
}
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
}
void BLINK(){
int cntr=5;
do
{
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
cntr = cntr-1;
}
while (cntr!=0);
digitalWrite(ledPin, HIGH);
delay(900000);
ledState = LOW;
}
void A(){
if (held < 5){
digitalWrite(ledPin, LOW);
}
}
void B(){
if (held > 5){
digitalWrite(ledPin, LOW);
}
}
答案 0 :(得分:1)
为什么不尝试用while循环来阻止这个过程
if(digitalread(buttonPin) == HIGH){
timerStart = millis();
while(digitalread(buttonPin == HIGH){}
timerEnd = millis();
}
然后比较timerStart和timerEnd