所以我在这里有一个程序,它具有三种不同的 LED 循环模式,代码在 if 语句中,我注意到即使 if 语句在 void 循环中,它也不再循环。我尝试在 void 循环内放置一个 void 循环,但它不起作用。我不能在一定程度上有代码循环,因为当我尝试它并将数量设置为 50 个循环时,即使用户在第三个循环中键入了另一个输入,它也会完成所有 50 个循环。我是 arduino 的新手,非常感谢有关如何解决此问题的新信息。这是我的代码:
int previousLED;
void setup(){
Serial.begin(9600);
Serial.println("Enter desired Pattern Number: ");
for(int i=2; i<=9; i++){
pinMode (i,OUTPUT);
}
}
void loop(){
if (Serial.available()){
char serialdata = Serial.read();
//Serial.print("You have entered Pattern ");
//Serial.println(serialdata);
//Serial.print(" in BINARY:");
//Serial.println(serialdata,BIN);
if (serialdata == '1'){
Serial.println("LED Pattern 1 has been triggered!");
//
for(int ledPin = 2; ledPin<=9; ledPin++){
digitalWrite(previousLED,LOW);
digitalWrite(ledPin,HIGH);
delay(200);
previousLED = ledPin;
}
for(int ledPin = 9; ledPin>=2; ledPin--){
digitalWrite(previousLED,LOW);
digitalWrite(ledPin,HIGH);
delay(200);
previousLED = ledPin;
}
}
if (serialdata == '2'){
Serial.println("LED Pattern 2 has been triggered!");
for(int ledPin = 2; ledPin<=9; ledPin++){
digitalWrite(ledPin,HIGH);
delay(700);
}
delay(3000);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(1000);
}
if (serialdata == '3'){
Serial.println("LED Pattern 3 has been triggered!");
for (int k = 0; k<=5; k++){ //
for(int ledPin = 2; ledPin<=9; ledPin++){
digitalWrite(ledPin, HIGH);
}
delay(200);
for(int ledPin = 2; ledPin<=9; ledPin++){
digitalWrite(ledPin, LOW);
}
delay(200);
} //
}
}
}