Arduino 正在闪烁红色 LED;在为黄色 LED 接受输入之前

Arduino is blinking Red LED; before taking input for Yellow LED

红色 LED 闪烁的输入可以是任何东西,例如4. 一旦我为红色 LED 提供输入:

1- 红色 LED 闪烁 4 次 2- 显示为黄色输入的消息 3- 在进入输入之前,红色 LED 开始闪烁

程序正在跳过黄色 LED。

int redLED;
int yellowLED;

int redLEDpin = 8;
int yellowLEDpin = 4;

void setup() {
    // put your setup code here, to run once:

    pinMode(redLEDpin ,OUTPUT);
    pinMode(yellowLEDpin, OUTPUT);

    Serial.begin(115200);
}

void loop() {
    // put your main code here, to run repeatedly:

    Serial.print("how many times would you like to blink red LED? ");
    while (Serial.available()==0){}

    redLED = Serial.parseInt();

    Serial.println(""); //Produce line gap between the Prompts

    Serial.print("how many times would you like to blink yellow LED? ");
    while (Serial.available()==0){}

    yellowLED = Serial.parseInt();

    for(int counter=1; counter<=redLED ; counter=counter+1){
        digitalWrite(redLEDpin, HIGH);
        delay(1000);
        digitalWrite(redLEDpin,LOW);
        delay(1000);
    }

    for(int countery=1; countery<=yellowLED ; countery=countery+1){
        digitalWrite(yellowLEDpin, HIGH);
        delay(750);
        digitalWrite(yellowLEDpin,LOW);
        delay(750);
    }
}

我相信您的串行监视器的行尾设置设置为 NL 和 CR。当您输入 4 时,4 + CR 会触发 redLED = Serial.parseInt();,而 NL 会触发 yellowLED = Serial.parseInt();。第二个 parseInt() 始终 returns 0 仅作为换行符(或仅回车 return)不是有效数字。尝试其他行尾设置。