How to get 2 inputs?

Hello, I am seeking some help on this very simple program I was asked to complete. I can't seem to have both my inputs received because after the 1st input, the program jumps straight into the loop (skipping me being able to input the second). I have tried a delay after the first input, but that doesn't seem to help with the system even being able to receive my 2nd input (I type 2nd value and its not recognizing). Any help? Apologies as I know this is probably super easy, I just began learning Arduino. Thank you.

Hardware: Arduino Uno

Instructions: In the Serial Monitor, the user should see the question. What is the first digit of your age? Then the Serial Monitor waits the user to enter one number. Let’s use m to refer to this number. After that, the user should see the question. What is the second digit of your age? Then the Serial Monitor waits the user to enter one number. Let’s use n to refer to this number. After the numbers are entered. The red LED should blink m times and the yellow LED should blink n times.

My Code:

int redLEDPin=8; //Declare redLEDPin an int, and set to pin 9 
int yellowLEDPin=10; //Declare yellowLEDPin an int, and set to pin 10 
int redOnTime=250; //Declare redOnTime an int, and set to 250 mseconds 
int redOffTime=250; //Declare redOffTime an int, and set to 250 
int yellowOnTime=250; //Declare yellowOnTime an int, and set to 250
int yellowOffTime=250; //Declare yellowOffTime an int, and set to 250
int m; // number of red blinks
int n; // number of yellow blinks

void setup() // put your setup code here, to run once:
{
  Serial.begin(9600);
  pinMode(redLEDPin, OUTPUT);  // Tell Arduino that redLEDPin is an output pin
  pinMode(yellowLEDPin, OUTPUT);  //Tell Arduino that yellowLEDPin is an output pin
}

void loop() // put your main code here, to run repeatedly:
{
  Serial.println("What is the first digit of your age?"); //Prompt User for Input
  while(Serial.available()==0) {} // Wait for User to Input Data  
  m = Serial.parseInt();  //Read the data the user has input

  Serial.println("What is the second digit of your age?"); //Prompt User for Input
  while(Serial.available()==0) {} // Wait for User to Input Data  
  n = Serial.parseInt();  //Read the data the user has input

  Serial.println("The Red LED is Blinking");
    for (int j=1; j<=m; j=j+1) {     // Start our for loop
      Serial.println(j);
      digitalWrite(redLEDPin,HIGH); //Turn red LED on
      delay(redOnTime);             //Leave on for redOnTime
      digitalWrite(redLEDPin,LOW);  //Turn red LED off
      delay(redOffTime);            //Leave off for redOffTime
  }
    Serial.println("The Yellow LED is Blinking");
    for (int j=1; j<=n; j=j+1) {     // Start our for loop
      Serial.println(j);
      digitalWrite(yellowLEDPin,HIGH); //Turn yellow LED on
      delay(yellowOnTime);             //Leave on for yellowOnTime
      digitalWrite(yellowLEDPin,LOW);  //Turn yellow LED off
      delay(yellowOffTime);            //Leave off for yellowOffTime
    } 
}

My Serial Monitor:
What is the first digit of your age?

What is the second digit of your age?

The Red LED is Blinking

1

2

The Yellow LED is Blinking

What is the first digit of your age?

Output:
Only the first Age is registered and corresponding LED Blinking

Who asked ? :thinking:


  while(Serial.available()==0) {} // Wait for User to Input Data  
  m = Serial.parseInt();  //Read the data the user has input

Hint

What goes in between the

{ }

Nothing?

@OP what is the serial monitor line ending control set to?

New Line and then 9600 baud

Maybe the OP should ask what is the purpose of the while loop and why they are blocking their code like this ?

So, change it to "no line ending"

That was it! Thank you so much! I knew it had to be easy, I guess I need to read up more. Can you explain what the serial monitor line ending control does? or why it would run as I had wanted with the last setting?

No, not really.

Take a look at Robin2's serial input handling topic to see how to do it properly.

will do, thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.