And so far we have learned the basics of Arduino.  Furthermore, nosotros have learned how to impress information to the Series Monitor through using serial.Print() function. If you would like to control the blinking of LEDs past using the Serial Monitor we need to modify the code from the for loop tutorial with some new statements.

In this tutorial, nosotros will continue to use the excursion created in the previous tutorial. If you demand any assistance with building this circuit, please get back and review that tutorial. Hither is the excursion diagram we are working inside this tutorial.

Parts you will demand

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 volition earn a commision if you lot buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

Example lawmaking: user input

          // Sketch: Blinking two LEDs past user int ledPin = 10; // declare pin10 as ledpin int numBlinks; // variable to shop the number of blinks String LedOnMessage = "Red LED is turned on"; // this is a cord with information String LedOffMessage = "Red LEd is turned off"; // this is a cord with data void setup() {   pinMode(ledPin, OUTPUT);  // gear up pin10 as output pin   digitalWrite(ledPin, LOW); // set the pin value on low at the begin   Series.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 information the user has input   for (int counter = one; counter <= numBlinks; counter++) {     Series.println(LedOnMessage);     digitalWrite(ledPin, Loftier);     delay(1000);     Series.println(LedOffMessage);     digitalWrite(ledPin, Low);     filibuster(1000);   }   Serial.print("The user has choosen the number:");   Serial.println(numBlinks);   Series.println(" "); }        

Code Explanation

Let united states look over the code above and review what we accept learned so far. At the top of the programme, we declare our variables and assign values to them. So in the void setup() role, we set our output pins and begin Serial communication. In the void loop() we accept established a for loops. One loop 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 glimmer

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

  1. Enquire the user for input
  2. Look for the user to enter the input through the Series 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 volition exist getting the parameters from the user. In this case, we still need to declare variables, but we do not demand 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 desire to get the number of the User from the Serial Monitor.

          Serial.println("How Many Times Practice You Want the Blood-red LEDs to glimmer?"); //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 yous. 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 washed by using a while loop. Rember, if we want to start a function nosotros need to start and end it with curly brackets. The program will execute what is betwixt the curly brackets until the status that is in the parenthesis is true.

The status role of the while loop states the following.

          while (Series.bachelor() == 0)        

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

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

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

Series.parsInt() reads the number of the user input, and and so the number is assigned to numBlinks. There are diverse commands for reading other variable types. For instance, if you want to read a string from the Serial Monitor you can utilise Series.readString(). The important aspect is that you need to utilize the right command for the type of data you want to read. In our case that volition exist an integer. If you lot run the lawmaking, remember to open the Serial Monitor to see the consequence. Below is a screenshot of how it should look when the users enter the number 3.