Hi All!
New to Arduino and new to the forum!
I am going through the basic tutorials and while writing a program with serial monitor input, I read / watched that to wait for using input you can use Serial.available() in an empty while loop to pause. I am trying to do this twice to get 2 pieces of input from the user. The program compiles and sends to Arduino. I get the first pause until input is entered, it goes to the second prompt for a moment and then immediately skips out of the second empty while loop to the remaining loops.
Code is:
int redLedPin = 8; // Set the Pin for the Red LED
int blueLedPin = 13; // Set the Pin for the Blue LED
int redLedOnTime = 200; // Set the On Time for the Red LED
int blueLedOnTime = 200; // Set the On TIme for the Blue LED
int redLedOffTime = 200; // Set the Delay Time for the Red LED
int blueLedOffTime = 200; // Set the Delay Time for the Blue LED
int redBlinkNum; // Set the Number of tImes to blink the red LED
int blueBlinkNum; // Set the Number of Times to blink the Blue LED
String redLedMsg = " The Red LED is Blinking ";
String blueLedMsg = " The Blue LED is Blinking ";
void setup() {
Serial.begin(9600); // Begin Serial Monitor at 9600 bits per second ( Baud Rate )
pinMode(redLedPin, OUTPUT); // Set the Pin mode for the Red LED as an OUTPUT
pinMode(blueLedPin, OUTPUT); // Set the Pin Mode for the Blue LED as an OUTPUT
}
void loop() {
Serial.println("Enter The Number Of Blinks for the Red LED: "); // Prompt User for Input
while (Serial.available()==0){ } // Wait for the User to Input Data
redBlinkNum = Serial.parseInt(); // Set the value of redBlinkNum to input data from user
Serial.println("Enter the Number of Blinks for the Blue LED: "); // Promt User for input again
while (Serial.available()==0){ } // Wait for Second Input from user
blueBlinkNum = Serial.parseInt(); // Set the value of blueBlinkNum to second input
Serial.println(redLedMsg);
for(int j=1; j <= redBlinkNum; j++) // Sets the Number of Times to blink RED LED
{ Serial.print("You Are On Blink # ");
Serial.println(j);
digitalWrite(redLedPin, HIGH); // Turn on the Red LED
delay(redLedOnTime); // Stay on for the amount of time specified above
digitalWrite(redLedPin, LOW); // Turn off the RED LED
delay(redLedOffTime); // Turn on for amount of time specified above
}
Serial.println(blueLedMsg);
for(int j=1; j <= blueBlinkNum; j++) // Sets the Number of Time to blink Blue LED
{ Serial.print("You Are On Blink # ");
Serial.println(j);
digitalWrite(blueLedPin, HIGH);
delay(blueLedOnTime);
digitalWrite(blueLedPin, LOW);
delay(blueLedOffTime);
}
}
If I hit a number ( int ) quickly after entering the first, the program runs as expected, re-prompts for the first input and quickly then skips to the second input prompt and waits.
Any Thoughts or assistance is appreciated.
Obviously it's not a crucial application, but I want to learn where I am going wrong or if there is a better way to gain multiple pieces of input from a user prior to running other loops.
Thanks!