按钮长按控制闪烁循环
Posted
技术标签:
【中文标题】按钮长按控制闪烁循环【英文标题】:button long-pressed to control the loop of blinking 【发布时间】:2014-10-04 11:40:22 【问题描述】:我已经在一个关于由按钮控制闪烁 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);
【问题讨论】:
【参考方案1】:你为什么不试着用一个while循环来阻止这个过程
if(digitalread(buttonPin) == HIGH)
timerStart = millis();
while(digitalread(buttonPin == HIGH)
timerEnd = millis();
然后比较 timerStart 和 timerEnd
【讨论】:
以上是关于按钮长按控制闪烁循环的主要内容,如果未能解决你的问题,请参考以下文章