Arduino Light 不会显示一次,而是连续显示

Arduino Light is not displayed once but in a continuous sequence

功能:

用户按下红色圆顶按钮红色圆顶按钮,假设按钮状态为高电平并且在串行监视器上,它应该每 100 毫秒打印“1”并在延迟 5 秒后:LED灯将处于高电平状态,点亮约 10 秒后,LED 灯将切换到低电平状态,即 LED 灯将熄灭。

因此流程:

正确行为:

初始状态->串行监视器显示“0” 当用户按下按钮时 -> 串行监视器每 100 毫秒显示“1”,并在延迟 10 秒后,LED 状态将为高电平。

延迟 10 秒后,LED 状态将为低电平,串行监视器显示仍为每 100 毫秒为“1”,表示红色圆顶按钮的按钮状态仍为高电平

问题:

当前行为: 初始状态-> 串口监视器显示“0” 当用户按下按钮时 -> 串口监视器显示单个“1”而不是连续显示“1”,但在延迟 10 秒后,LED 状态将变为高电平。

延迟 10 秒后,LED 状态将为低电平。此时,LED 不应再次变为高电平,但是,在延迟 10 秒后,LED 状态变为高电平,并在 10 秒后变为低电平。然后它变成了一个循环。 串行监视器显示仍为“1”,表明红色圆顶按钮的按钮状态仍处于高电平

因此,如何使一旦按下按钮,它将显示一个连续的“1”并延迟 10 秒,LED 将处于高电平状态,再延迟 10 秒,LED 状态会很低。即使按钮状态处于高电平,LED 仍将保持低电平状态

代码:

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 5s turn on
    break;

  case 200:
    digitalWrite(Relay, LOW); // after 10s turn off
    break;

  case 102: // small loop at the end, to do not repeat the cycle
    btnCnt--;
    break;    
}

Serial.println("1");
}else{
   Serial.println("0");
   if (btnCnt > 0) {  
     // disable all:
     stateLED = LOW;
     digitalWrite(Relay, LOW);
 }
  btnCnt = 0;
 }

 delay(100);
}

当您有 delay(10000) 然后 delay(2000); 时,您期望什么?如果您等待了这么长时间,它什么时候应该每 100 毫秒打印一次这些“1”?

您的 outputState 在按钮更改时更改,但您可以直接使用按钮状态跳过该部分 - 它完全相同。

我可以想象出类似的东西(未经测试,这只是概念):

const int buttonPin = 2;
const int Relay     = 4;

uint8_t    stateLED = LOW;
uint8_t      btnCnt = 1;

void loop() {
  if (digitalRead(buttonPin) == HIGH) {

    switch (btnCnt++) {
      case 0: case 1:
        stateLED = HIGH; // no idea, why is that in original code, but whatever
        break;

      case 50:
        stateLED = LOW;
        digitalWrite(Relay, HIGH); // after 5s turn on
        break;

      case 100:
        digitalWrite(Relay, LOW); // after 10s turn off
        break;

      case 102: // small loop at the end, to do not repeat the cycle
        btnCnt--;
        break;    
    }

    Serial.println("1");

  } else {

    if (btnCnt > 0) {  
      Serial.println("0");
      // disable all:
      stateLED = LOW;
      digitalWrite(Relay, LOW);
    }
    btnCnt = 0;

  }

  delay(100);
}

void setup() {
  Serial.begin(57600);
  pinMode(buttonPin, INPUT); 
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
}