用户按下红色圆顶按钮(不是2状态的按钮),因此它必须在所述第一压切换“ 0“ - >”1“&再次按下”1“ - >”0“时。
因此,按下按钮时,串行监视器将每100毫秒从“0”中打印“1”。行为表示buttonState从低电平切换到高电平
此外,LED条纹也连接到arduino。因此,当串行监视器中的按钮状态显示为高电平时,LED状态将在延迟10秒后切换到高电平,并在切换到低电平状态之前保持高电平状态10秒。
最后,buttonState在延迟(25s)之后应该从HIGH切换到LOW,而无需用户按下按钮。
问题:
在这一点上,用户必须按下红色圆顶按钮LOW & HIGH状态之间切换。因此,当按钮初始状态为低电平时,串行监视器显示“0”,按下时按钮将切换为高电平,在串行监视器中显示“1”,再次按下按钮时,按钮将从从高到低,串行监视器显示“0”。
因此,我想要求帮助如何让按钮状态从高切换到低,而无需用户按下按钮。
因此, correctBehaviour:
初始状态:“0”表示在串行监控,并且当用户按下按钮buttonstate示出的“1”中的串行监控和后25S的计数,按钮状态将切换到低,而无需用户再次按下按钮。
代码:
const int buttonPin = 2; //the number of the pushbutton pin
const int Relay = 4; //the number of the LED relay pin
uint8_t stateLED = LOW;
uint8_t btnCnt = 1;
int buttonState = 0; //variable for reading the pushbutton status
int buttonLastState = 0;
int outputState = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// Check if there is a change from LOW to HIGH
if (buttonLastState == LOW && buttonState == HIGH)
{
outputState = !outputState; // Change outputState
}
buttonLastState = buttonState; //Set the button's last state
// Print the output
if (outputState)
{
switch (btnCnt++) {
case 100:
stateLED = LOW;
digitalWrite(Relay, HIGH); // after 10s turn on
break;
case 200:
digitalWrite(Relay, LOW); // after 20s turn off
break;
case 202: // small loop at the end, to do not repeat the LED cycle
btnCnt--;
break;
}
Serial.println("1");
}else{
Serial.println("0");
if (btnCnt > 0) {
// disable all:
stateLED = LOW;
digitalWrite(Relay, LOW);
}
btnCnt = 0;
}
delay(100);
}
你是什么意思“,但没有这样做正确使用,如果平均(毫秒() - lastpressed> 20000){...“,我使用延迟(100)是检查每个按钮,但按。对不起,我没有真正明白你的意思 – Luke
阅读,锻炼并了解BlinkWithoutDelay示例https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay。这不仅仅是闪烁,而是关于每个非阻塞等待。 – datafiddler