Made new sketch but can't figure out why the Serial.print is outputting twice

Hi,

I'm new to Arduino and coding. I created a sketch that is intended to ask a user for a number and blink an led that number of times. The sketch works but for some reason, after the first run, it asks for the user input twice. I am using the Arduino Uno R3 with the Arduino Sensor Kit Base. Code is below:

int numBlinks;
String msg="How Many Blinks?";
const int led=6;
int delTime=500;

void setup() {
  Serial.begin(115200);
  pinMode(led,OUTPUT);
}

void loop() {
  Serial.println(msg);            //ask user for the number of blinks

  while (Serial.available()==0) { //wait for input from user
  }

  numBlinks=Serial.parseInt();    //get number of blinks from serial monitor

  while (numBlinks>0) {           //blink led from number of blinks down to zero
    digitalWrite(led,HIGH);
    delay(delTime);
    digitalWrite(led,LOW);
    delay(delTime);
    numBlinks=numBlinks-1;
  }
}

What am I doing wrong here?

Thanks for any Help,
Randy

make sure the Serial terminal is set to not send any CR nor LF at the end of what you type.

My guess would be that you have a Line ending set in the Serial monitor so after the parseInt() there Line ending is still in the input buffer so Serial.available() is not zero

Try setting the Line ending to "No Line ending"

Thanks all for the help. It was the setting in the serial monitor. Setting it to "No line ending" did the trick.

if you want to understand what was happening

  • you typed 123 in the Serial monitor which actually sent "123\r\n"

  • the arduino sees the '1' and ends the waiting while loop since something was available in Serial

  • the function Serial.parseInt() got called which read the '1', the '2' and the '3' and then sees (but does not remove) the '\r' which is not part of an integral number, so stopped there, leaving the "\r\n" in the Serial buffer

  • the function returns 123 which you play with and the loop loops

  • the arduino sees the '\r' and ended the waiting while loop since something was available in Serial

  • the function Serial.parseInt() got called which ignores the '\r' and the '\n' as this is the default behavior to skip spaces at the beginning that are not part of an integral number, and waits for an integral number to arrive. Nothing comes in so the function times out and return 0

  • this is the 0 which you play with and the loop loops

there is nothing left in the Serial buffer, so the waiting while loop does it jobs, it waits.

➜ those functions like Serial.parseInt() come handy but they are not fool proof. If you want robust and non blocking code, I would suggest to study Serial Input Basics to handle this

Thanks for the explanation. It helps and I will definitely check out Serial input Basics.

if you want to see what's going on, try this with the Serial monitor sending CR or LF or CR+LF

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

void loop() {
  Serial.println("Enter a number");
  while (Serial.available() == 0);
  long v = Serial.parseInt();
  Serial.print("got: "); Serial.println(v);
  if (Serial.available()) {
    Serial.println("stuff left in the input buffer after reading the number");
    while (Serial.available() != 0) {
      int r = Serial.read();
      switch (r) {
        case '\r':  Serial.println("CR"); break;
        case '\n':  Serial.println("LF"); break;
        default: Serial.print("0x"); Serial.print(r, HEX); break;
      }
    }
  }
}

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