Arduino - 如何停止循环并等待下一个命令?

Arduino - How do I stop loop and wait for next command?

我希望我的代码在代码的第一部分之后停止并等待直到开关改变状态...我似乎找不到执行此操作的命令!任何帮助表示赞赏。 我已将代码标记到一半,我希望它停止的地方。

int switchState = 0;
void setup() {
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(2,INPUT);
}

void loop() {
  switchState = digitalRead(2);
  if (switchState == LOW) {
    // The button is not pressed
    delay(250);
    digitalWrite(3, HIGH);
    delay(250);
    digitalWrite(4, HIGH);
    delay(250);
    digitalWrite(5, HIGH);
    delay(500);
    digitalWrite(3, LOW);
    delay(250);
    digitalWrite(4, LOW);
    delay(250);
    digitalWrite(5, LOW);
    delay(1000);
    digitalWrite(3, HIGH); // green LED
    digitalWrite(4, HIGH); // red LED
    digitalWrite(5, HIGH); // red LED
    delay(100);
    digitalWrite(3, LOW); // green LED
    digitalWrite(4, LOW); // red LED
    digitalWrite(5, LOW); // red LED
    delay(100);
    digitalWrite(3, HIGH); // green LED
    digitalWrite(4, HIGH); // red LED
    digitalWrite(5, HIGH); // red LED
    delay(100);
    digitalWrite(3, LOW); // green LED
    digitalWrite(4, LOW); // red LED
    digitalWrite(5, LOW); // red LED
    delay(100);
    digitalWrite(3, HIGH); // green LED
    digitalWrite(4, HIGH); // red LED
    digitalWrite(5, HIGH); // red LED
    delay(100);
    digitalWrite(3, LOW); // green LED
    digitalWrite(4, LOW); // red LED
    digitalWrite(5, LOW); // red LED
    delay(100);
    digitalWrite(3, HIGH); // green LED
    digitalWrite(4, HIGH); // red LED
    digitalWrite(5, HIGH); // red LED
    delay(100);
    digitalWrite(3, LOW); // green LED
    digitalWrite(4, LOW); // red LED
    digitalWrite(5, LOW); // red LED
    delay(1000);
    digitalWrite(3, HIGH); // green LED
    digitalWrite(4, LOW); // red LED
    digitalWrite(5, LOW); // red LED
// HERE IS WHERE I WANT TO HALT THE EXECUTION OF THE FIRST PART OF THE CODE
  }
  else {
    digitalWrite(3, LOW); // green LED
    digitalWrite(4, HIGH); // red LED
    digitalWrite(5, HIGH); // red LED
    delay(250); // wait quarter of a second
    digitalWrite(4, HIGH); // red LED
    digitalWrite(5, LOW); // red LED
    delay(250); // wait quarter of a second
  }

} // go back to beginning of loo

我希望我的代码在代码的第一部分之后停止并等待直到开关改变状态...我似乎找不到执行此操作的命令!任何帮助表示赞赏。 我已将代码标记到一半,我希望它停止的地方。

您可以使用 last 状态变量轻松实现此目的:

int switchState = 0;
int lastSwitchState = 2;

void setup() {
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(2,INPUT);
}

void loop() {
  switchState = digitalRead(2);
  if (switchState != lastSwitchState) {  
    if (switchState == LOW) {
        // The button is not pressed
        delay(250);
        digitalWrite(3, HIGH);
        // ...
        // HERE IS WHERE I WANT TO HALT THE EXECUTION OF THE FIRST PART OF THE CODE
        lastSwitchState = switchState;
    } else {
        // ...  
        digitalWrite(5, LOW); // red LED
        delay(250); // wait quarter of a second
        lastSwitchState = switchState;
    }
  } else {
    // Implement powersave state or idle procedure  
  }
} // go back to beginning of the loop

此代码只是检查当前值是否等于上一个值。如果它们不同,它只会执行您的核心代码。如果它们(switchStatelastSwitchState)相同,您可以进入节能程序或进入无限循环。因此,除非值已更改,否则您的循环将在下一次迭代时“停止”。