Setting the Line Ending

When you run any of the following sketches, note the setting for the line ending
near the bottom right of the Serial Console. Make sure it is set to No line ending.
The downside of the other options is discussed in the section on
getting strings.

Getting Integer Data

In this example, the sketch prompts the user for an integer input, stores
the integer in a variable of type long int, and then displays the integer it received.

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

void loop() {
    Serial.println("Enter an integer");       // Prompt for input
long int n = Serial.parseInt();           // Wait for an integer and then store it
    Serial.print("Thanks.  You entered: ");   // Display the value that had been input
Serial.println(n);   
}

You will probably find that the sketch prompts you over and over again
for an integer. Unless you are very, very quick, you won’t be able to
enter the integer before the program asks you again. Each time you don’t
enter fast enough, the parseInt function will report that you actuall
did enter the value 0.

You can fix this by telling the microprocessor to wait until you have
sent your input before it tries to parse that input into an int.
To test whether input is available, use the command Serial.available(),
which returns the number of bytes available from the input for reading.

There are 2 ways to do use Serial.available(). One is to enclose your
parseInt command in an if statement:

Serial.println("Enter an integer");           // Prompt for input
if (Serial.available()) {                     // Enter this block only if Serial data is ready
long int n = Serial.parseInt();           // Wait for an integer and then store it
Serial.print("Thanks.  You entered: ");   // Display the value that had been input
Serial.println(n);
}

The other way to use Serial.available() is to precede your parseInt
command with a single while statement that executes an empty loop (i.e.,
does nothing) until some input data is available.

Serial.println("Enter an integer");       // Prompt for input
while (!Serial.available()) { }           // while Serial data is not (!) ready, do nothing ({})
long int n = Serial.parseInt();           // Wait for an integer and then store it
Serial.print("Thanks.  You entered: ");   // Display the value that had been input
Serial.println(n);

If you choose the if statement, then when input is not available, the
microprocessor will just go on to the statement following the block, wasting
little time.

If you choose the while statement, then when input is not available, the
microprocessor simply waits and does nothing.

There are some applications where the if statement is the right thing to do,
and other applications where while is better. In cases where your sketch
requires 2 or more inputs, you must use the while statement, because otherwise
you won’t be able to control which of the inputs you are providing.

What happens if you mistakenly enter some character which does not belong in an int.
Not to worry: the parseInt function strips any leading characters that do not belong
in an int, and then rejects any characters following one that does not below
in an int. So if you mistakenly entered: abc123abc, then parseInt would
conclude that you meant to enter 123, and would simply throw away the rest
of your input.

Getting Floating Point Data

In this example, the sketch asks for, stores, and displays an integer as before,
but then also asks for a floating point number. This number is stored in
a variable of type float, and then displayed.

void setup() {
    Serial.begin(57600);
while (!Serial) {};
}

void loop() {
    Serial.println("Enter an integer: ");
    while (!Serial.available()) {}      
    long int inum = Serial.parseInt();   
    Serial.print("Value received: ");   
    Serial.println(inum);

Serial.println("Enter a floating point number: ");
    while (!Serial.available()) {}
    float fnum = Serial.parseFloat();  // read input, parse it as float, and then store it 
    Serial.print("Value received: ");   
    Serial.println(fnum);
}

As in the case of parseInt, the parseFloat function strips any leading characters
that do not belong in an float, and then rejects any characters following one
that does not below in an float. So if you mistakenly entered: abc12.-3abc,
then parseFloat would conclude that you meant to enter 123, and would convert
that number to a float and simply throw away the rest of your input.

Getting String Data

In this example, the sketch asks for, stores, and displays an integer and a float
as before, but then also asks for some text input. This text is stored as a
variable of type String, and then displayed.

void setup() {
    Serial.begin(57600);
while (!Serial) {};
}

void loop() {
    Serial.println("Enter an integer: ");
    while (!Serial.available()) {}      // Wait for input
    long int inum = Serial.parseInt();   
    Serial.print("Value received: ");   
    Serial.println(inum);

Serial.println("Enter a floating point number: ");
    while (!Serial.available()) {}
    float fnum = Serial.parseFloat();
    Serial.print("Value received: ");   
    Serial.println(fnum);

Serial.println("Enter some text: ");
    while (!Serial.available()) {}
    String s = Serial.readString();    // read input as text and store it as a String
    Serial.print("Value received: ");   
    Serial.println(s);
}

Fixing Mistakes

(TODO: explain how to remove unwanted trailing data from the input buffer.)

Putting input data to use

In the following sketch, the user is asked how many times he wants to blink the
on-board LED, and of course the Arduino dutifully responds by blinking that LED
for the specified number of times. When you run this sketch, be mindful that many
Arduino boards actually have 3 different LEDs on board:

  1. An LED which is always on when the board is powered.
  2. A tiny LED which flashes briefly when the board transmits or receives data
    through its serial input.
  3. A diagnostic LED that can be used, e.g., for test purposes.

It is the last of these, the diagnostic LED (also called LED_BUILTIN, which is
being blinked in the following sketch.

int LED = 13;

void setup() {
    Serial.begin(57600);
    pinMode(LED, OUTPUT);
    digitalWrite(LED, LOW);  
}

void loop() {
    Serial.println("How many blinks? ");

    while ( ! Serial.available()) {}

    long int num = Serial.parseInt();
    for (int i=1; i <= num; i++) {
      digitalWrite(LED, HIGH);
  delay(250);
      digitalWrite(LED, LOW);
      delay(500);
    }
}