Ahmed Qayyum
Ahmed Qayyum

Reputation: 137

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

Input for Red LED Blinking would be anything e.g. 4. Once I give an input for the Red LED:

1- Red LED blinks 4 times 2- Displays the message to give the input for Yellow 3- Before entering the Input, the Red LED starts blinking

And the program is skipping Yellow 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);
    }
}

Upvotes: 0

Views: 133

Answers (1)

Kentaro Okuda
Kentaro Okuda

Reputation: 1592

I believe your Serial Monitor's line ending setting is set to Both NL & CR. When you enter 4, 4 + CR triggers redLED = Serial.parseInt(); and NL triggers yellowLED = Serial.parseInt();. And the second parseInt() always returns 0 as newline only (or carriage return only) is not valid digits. Try other line ending settings.

Upvotes: 1

Related Questions