1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Leonardo Problem

Discussion in 'Programming/Internet' started by bigmike333, Oct 8, 2018.

  1. bigmike333

    bigmike333 Guest

    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.

    Login To add answer/comment
     

Share This Page