Learn to code

ARDUINO & CODES

Previous Post
Sending information to the serial monitor using serial print() with Arduino
Next Post
Arduino Tutorial: 4.0 Introduction into digital / analog pins

Getting user input from the serial monitor with Arduino

in Arduino Serial Communication

So far we have learned the basics of Arduino.  Furthermore, we have learned how to print information to the Serial Monitor through using serial.Print() function. If you would like to control the blinking of LEDs by using the Serial Monitor we need to modify the code from Arduino Tutorial: 2.5 For loop/repeat a statement by some new statements.

In this tutorial, we will continue to use the circuit created in the previous tutorial. If you need any help with building this circuit, please go back and review that tutorial. Here is the circuit diagram we are working with this tutorial.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
220 ohm resistor 220 ohm resistor × 1
Dupont Wires Dupont Wires × 2
LED LED × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

Example code: user input

// Sketch: Blinking two LEDs by user
int ledPin = 10; // declare pin10 as ledpin
int numBlinks; // variable to store the number of blinks
String LedOnMessage = "Red LED is turned on"; // this is a string with information
String LedOffMessage = "Red LEd is turned off"; // this is a string with information
void setup()
{
  pinMode(ledPin, OUTPUT);  // set pin10 as output pin
  digitalWrite(ledPin, LOW); // set the pin value on low at the begin
  Serial.begin(9600);
}
void loop()
{
  Serial.println("How Many Times Do You Want the Red LEDs to blink?"); //Prompt User for Input
  while (Serial.available() == 0) {
    // Wait for User to Input Data
  }
  numBlinks = Serial.parseInt(); //Read the data the user has input
  for (int counter = 1; counter <= numBlinks; counter++) {
    Serial.println(LedOnMessage);
    digitalWrite(ledPin, HIGH);
    delay(1000);
    Serial.println(LedOffMessage);
    digitalWrite(ledPin, LOW);
    delay(1000);
  }
  Serial.print("The user has choosen the number:");
  Serial.println(numBlinks);
  Serial.println(" ");

Code Explanation

Let us look over the code above and review what we have learned so far. At the top of the program, we declare our variables and assign values to them. Then in the void setup() part, we set our output pins and begin Serial communication. In the void loop() we have established a for loops. One loop that repeats until it reaches the number that is specified by the user. The parameter used in this loop is how many times you want the LEDs to blink

In order to get the input from the user, we will need to make sure that we turned on the Serial port in our void setup() part. Now, in order to get the user input, we need to do a couple of things.

  1. Ask the user for input
  2. Wait for the user to enter the input through the Serial Monitor.
  3. Read the information from the Serial Port.
  4. Do something with this information

In contrast to the previous tutorial, 2.5 Understanding for loops in Arduino, where we coded the number of blink times, we will be getting the parameters from the user. In this case, we still need to declare variables, but we do not need to assign values to them. Hence the following adjustments to the code derived from the previous tutorial.

int numBlinks; // variable to store the number of blinks

If you look carefully, in our previous tutorial we assigned a value to numBlinks.

Now in our void loop(), we want to get the number of the User from the Serial Monitor.

Serial.println("How Many Times Do You Want the Red LEDs to blink?"); //Prompt User for Input
  while (Serial.available() == 0) {
    // Wait for User to Input Data
  }
  numBlinks = Serial.parseInt(); //Read the data the user has input

There are a lot of new things in the lines above. The first line should be familiar to you. We are printing a question to Serial Monitor. Since the Arduino is much faster than a person the Arduino should be waiting for the input of the User. This is done by using a while loop. Rember, if we want to start a function we need to start and end it with curly brackets. The program will execute what is between the curly brackets until the condition that is in the parenthesis is true.

The condition part of the while loop states the following.

while (Serial.available() == 0)

Serial.available () is a function that when you call it, it returns a “1” if the user inputs any data. If the User did not enter any data the function returns a “0”.  We are using the comparison operator == (equal to) to ensure that the program will not continue until the User has given their input.

Now that the User has given their input, we need to read this input. This is done in the next line of codes.

numBlinks = Serial.parseInt(); //Read the data the user has input

Serial.parsInt() reads the number of the user input, and then the number is assigned to numBlinks. There are various commands for reading other variable types. For instance, if you want to read a string from the Serial Monitor you can use Serial.readString(). The important aspect is that you need to use the correct command for the type of data you want to read. In our case that will be an integer. If you run the code, remember to open the Serial Monitor to see the result. Below is a screenshot of how it should look when the users enter the number 3.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu