Double talk.

I have boiled this down to a very simple piece of code:

int testKey;

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("Input Source ID "); //Ask me the number
while (Serial.available()==0){ } //Wait for me to answer
testKey = Serial.parseInt(); //Store my answer in this variable
Serial.print("Parsed Input is "); //Print the results on serial monitor
Serial.println(testKey);
}

When I run the code on my Leonardo, or Uno, I expect to have Input Source ID to be printed on the serial monitor. It remains blank (it is set to 9600 baud). If I go ahead and enter a 1, then the serial monitor starts working. However, from this point on, it seems to ask me for the ID again, and print a 0 to the serial monitor:

Parsed Input is 1
Input Source ID
Parsed Input is 0
Input Source ID

It then is waiting for me to enter a new Source ID. If I then enter a 2, this is what I get, in addition to what was already there.

Parsed Input is 1
Input Source ID
Parsed Input is 0
Input Source ID
Parsed Input is 2
Input Source ID
Parsed Input is 0
Input Source ID

My question is, why doesn't the void loop start automatically and immediately prompt me for input? Secondly, after I give it a number, it seems to run it twice, inserting a 0 into the variable. Why? This seems so straightforward, but it has me stumped. I tried some delay statements here and there, to no avail. Thanks in advance.

How fast can you type?

Please remember to use code tags when posting code.

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

Also, there is a simple user-input example in Planning and Implementing a Program

...R

after I give it a number

Are you typing, say, a “1” followed by “Enter”?

In that case, your code would return the value 1 for the digit 1 and 0 for the Enter character(s).

On the Leonardo you are supposed to have "while (!Serial);" after Serial.begin(). This is to wait until the USB connection is established. That might be why the first "Input Source ID " is lost. It is being sent before the USB connection is established. On a non-Leonardo the added line is harmless so you can put it in every sketch.