Serial.available issues

I am trying to program my Arduino Mega 2560 to blink a red and green light a number of times provided by the user into the Serial Monitor. The Serial monitor asks how many times to blink the red light, waits for input, blinks the appropriate number of times, then asks how many times to blink the green light, but the program does not wait for user input and goes back to asking about the red light and waiting. Where have I gone wrong? I want the program to stop and wait for my input on how many times the green light should blink.

Jan_04_19_Blinking_Light.ino (1.91 KB)

You either need to set the Serial monitor line ending menu near the bottom right corner to "No line ending", or else write your code so that it can handle any line ending setting.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.



Post#1 due to @pert says that --

1. The current 'line ending' tab setting of your Serial Monitor is active, and it could be like 'Newline' or 'Carriage return' or 'Both NL & CR' as is shown below.

2. Put the 'line ending' tab into inactive state by bringing in the 'No line ending' tab which is shown below.