Arduino闪烁红色LED;在输入黄色LED之前

时间:2019-07-22 18:20:00

标签: arduino serial-port arduino-uno arduino-ide

红色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);
    }
}

1 个答案:

答案 0 :(得分:1)

我相信您的串行监视器的行结束设置被设置为NL&CR。输入4时,4 + CR触发redLED = Serial.parseInt();,而NL触发yellowLED = Serial.parseInt();。第二个parseInt()总是返回0,因为仅换行符(或仅回车)是无效数字。尝试其他行尾设置。