So far we have learned the basics of Arduino.  Furthermore, nosotros have learned how to print data to the Serial Monitor through using serial.Impress() function. If y'all would like to command the blinking of LEDs by using the Serial Monitor nosotros need to modify the code from the for loop tutorial with some new statements.

In this tutorial, nosotros will go along to employ the excursion created in the previous tutorial. If you demand whatsoever assist with edifice this circuit, please go back and review that tutorial. Here is the excursion diagram nosotros are working within this tutorial.

Parts you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 2x
Breadboard 400 points 1x
220Ω resistor 1x
Led 1x

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 in a higher place.

BreadBoard Layout

Example code: user input

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

Lawmaking Explanation

Let usa expect over the lawmaking above and review what we have learned so far. At the acme of the plan, we declare our variables and assign values to them. And then in the void setup() part, we ready our output pins and brainstorm Serial communication. In the void loop() we have established a for loops. One loop repeats until it reaches the number that is specified past the user. The parameter used in this loop is how many times yous desire the LEDs to glimmer

In lodge to get the input from the user, nosotros will need to make sure that we turned on the Serial port in our void setup() part. Now, in order to go 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. Exercise something with this information

In contrast to the previous tutorial, two.five 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, nosotros still need to declare variables, only we exercise not need to assign values to them. Hence the post-obit adjustments to the lawmaking derived from the previous tutorial.

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

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

At present in our void loop(), nosotros want to get the number of the User from the Serial Monitor.

          Serial.println("How Many Times Do Yous Want the Red LEDs to blink?"); //Prompt User for Input   while (Series.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 starting time line should be familiar to yous. Nosotros are printing a question to Series Monitor. Since the Arduino is much faster than a person the Arduino should exist waiting for the input of the User. This is washed by using a while loop. Rember, if we want to start a function we demand to start and end it with curly brackets. The program volition execute what is betwixt the curly brackets until the status that is in the parenthesis is truthful.

The condition function of the while loop states the following.

          while (Series.available() == 0)        

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

At present that the User has given their input, we need to read this input. This is washed in the side by side line of codes.

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

Serial.parsInt() reads the number of the user input, and and then the number is assigned to numBlinks. At that place are diverse commands for reading other variable types. For example, if yous want to read a string from the Serial Monitor y'all can use Serial.readString(). The important aspect is that yous need to employ the correct command for the blazon of data you desire to read. In our instance that volition be an integer. If y'all run the code, remember to open the Serial Monitor to encounter the issue. Below is a screenshot of how it should look when the users enter the number 3.