Arduino LESSON 6: Reading From the Serial Port

Arduino LESSON 6: Reading From the Serial Port

Arduino LESSON 6: Reading From the Serial Port

So far in our programming, we've established the variables within the programme, generally at the top. We would alter the lines of code that set those variables to vary the amount of times the LEDs blink. This is OK for fun, but if you want other people to utilise your applications, you can't have them messing around with your code. You must be able to obtain user input without changing the code. We may accomplish this by use the Serial Port. We can use the Serial Port to publish information to the user, but we can also use it to collect information from the user.

We will continue to use the circuit developed in Arduino Lesson 3 in these tasks. Go back and reread that lesson if you need help putting the circuit together. Here's a schematic of the circuit we're working on.

This circuit will allow you to independently control two Light Emitting Diodes from the arduino microcontroller

As a reminder, this is the code we've been working with that integrates everything we've learned thus far. I'm adding this code so you may refer to it if you get stuck and so you can use it as an example of your work. To learn programming, you must write your own code, make mistakes, and then identify and rectify your faults. You will not learn programming if you merely copy and paste my code through these classes.

int redLEDPin=9; //Declare redLEDPin an int, and set to pin 9 
int yellowLEDPin=10; //Declare yellowLEDPin an int, and set to pin 10 
int redOnTime=250; //Declare redOnTime an int, and set to 250 mseconds 
int redOffTime=250; //Declare redOffTime an int, and set to 250 
int yellowOnTime=250; //Declare yellowOnTime an int, and set to 250
int yellowOffTime=250; //Declare yellowOffTime an int, and set to 250
int numYellowBlinks=5; //Number of times to blink yellow LED
int numRedBlinks=5;  //Number of times to blink red LED
String redMessage="The Red LED is Blinking"; //Declaring a String Variable 
String yellowMessage="The Yellow LED is Blinking"; //Declaring a String Variable

void setup() {
  Serial.begin(115200);        // Turn on the Serial Port
  pinMode(redLEDPin, OUTPUT);  // Tell Arduino that redLEDPin is an output pin
  pinMode(yellowLEDPin, OUTPUT);  //Tell Arduino that yellowLEDPin is an output pin
}

void loop() {

Serial.println(redMessage);
  for (int j=1; j<=numRedBlinks; j=j+1) {     // Start our for loop
    Serial.print("   You are on Blink #: ");
    Serial.println(j);
    digitalWrite(redLEDPin,HIGH); //Turn red LED on
    delay(redOnTime);             //Leave on for redOnTime
    digitalWrite(redLEDPin,LOW);  //Turn red LED off
    delay(redOffTime);            //Leave off for redOffTime
}
Serial.println(" ");
Serial.println(yellowMessage);
  for (int j=1; j<=numYellowBlinks; j=j+1) {     // Start our for loop
    Serial.print("   You are on Blink #: ");
    Serial.println(j);
    digitalWrite(yellowLEDPin,HIGH); //Turn yellow LED on
    delay(yellowOnTime);             //Leave on for yellowOnTime
    digitalWrite(yellowLEDPin,LOW);  //Turn yellow LED off
    delay(yellowOffTime);            //Leave off for yellowOffTime
}
Serial.println(" ");
}

OK, have a look at this code and go over what we've learnt so far. We define our variables and give values to them at the beginning of the programme. So far, we've worked with variables of the types int and String. Then, in the void loop, we launch our serial port and set our two Arduino pins to OUTPUT. We created two for loops in the void loop... one to flash the red LED and one to blink the yellow LED. The for loop parameters, such as how many times to blink and how long each blink should last, are all defined at the start of the programme.

As we said at the outset, it's fine to start out this way, but at some point, you should be obtaining your parameter values from the user rather than hard coding them into the programme. You want the software to ask the user how many times he wants the red LED to blink, and then how many times he wants the yellow LED to blink. This is actually rather simple, and we do it through the serial port, which is quite similar to how we learnt to print in Lesson 5.

You must ensure that your serial port is turned on in your void configuration in order to receive user input. As seen in the code above, you do this with a Serial.begin(9600); instruction. If you want to print to or read from the serial port, you must always include this command in your void setup(). Now, in order to obtain user feedback, you must accomplish three things:

  • Request Input from the User
  • Allow time for the user to enter the input.
  • Read the data from the serial port.

In the preceding software, let us suppose that in our empty loop, we want to question the user for how many times he wants the red LED to blink, followed by how many times he wants the yellow LED to blink. Instead of hardwiring the parameters into the programme, we now obtain them from the user in this situation. In this scenario, we must still declare our variables, but we do not need to assign them values. As a result, in the above example, the code:

int numYellowBlinks=5; //Number of times to blink yellow LED
int numRedBlinks=5;  //Number of times to blink red LED

Should be removed and replaced with:

int numYellowBlinks; //Number of times to blink yellow LED
int numRedBlinks;  //Number of times to blink red LED

Now, all we're doing is declaring our variables. We are not setting values to them because the values will be provided by the user. It is essential to note, however, that any variables that we will utilise must still be declared.

Now, in our void loop(), here is the code that will ask the user how many times he wants the LED to flash.

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

There's a lot going on in these few lines of code, and there's a lot for you to learn, so let's dissect it carefully. You should be familiar with the first line. You're just printing a message on the serial monitor informing the user that you're waiting for input. In this scenario, he will receive a notice requesting that he enter a number. Now, keep in mind that a computer can operate much quicker than a human, therefore when a computer asks a person to do anything, the computer must sit and wait for the person to do it. This is accomplished in the second and third lines of code listed above. A while loop is demonstrated here. As you can see, the while loop includes a clause that begins and ends with curly brackets. As long as the condition in the parenthesis is true, the programme will continue to execute what is between these curly brackets. So a while loop is similar to a for loop, except that the while loop can repeat indefinitely as long as the condition in the parenthesis is true. As a result, we must comprehend the 'condition' portion of the code above, which is Serial.available()==0. This is not as perplexing as it appears. When you call Serial.available(), it returns a '1' if the user has entered data and a '0' if the user has not input data. So, if the user does not enter any data, Seraial.available() returns 0, the condition is true, and the programme remains in the while loop.

So the bottom line is that

while(Serial.available()==0) { }

will sit and loop indefinitely, waiting for the user to enter data When the user enters the data, the programme will proceed to the following lines of code. Because there are no instructions between the two curly brackets, we do nothing in the while loop. The goal of this line of code is to simply keep the programme at that place till the user enters his data.

When the user enters the data, Serial.avaialbe() returns '1' and the program continues to the next line of code. The following line of code,

 numRedBlinks=Serial.parseInt();

This is the line that reads the data. Serial.parseInt() reads the number entered by the user and assigns it to the variable numRedBlinks. You use Serial.parseInt to read an integer from the serial port (). Other variable types can be read using various instructions. If you were reading a floating number, you would probably use Serial.parseFloat (). To read a string, use Serial.readString (). The essential thing to remember is that you must use the right command for the type of data you are attempting to read.

Put that code in your application at the start of the void loop(), and also write a similar sequence of code to receive the numYellowBlinks from the user. After you've gotten it functioning, you may ask the user for other parameters, such as the different blink time variables. You should experiment with this and get used to receiving user input. When you execute the code, be sure to click the icon in the upper right corner of the Arduino IDE to bring up the serial monitor. The prompt should appear in the Serial Monitor, and you should input your number in the tiny box before clicking transmit or pressing the enter key.

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0