Книга: Arduino: Master The Arduino Basics
Назад: Chapter 6: USB and Serial Communication on Arduino
Дальше: Chapter 7: Can Arduino Work Wirelessly?

Before we are able to get started with this kind of communication, we need to have a good idea of what serial communication is all about. This type of communication is going to work on a series of 1s and 0s or a binary system. The Arduino board will be able to send these bits (which are the 1s and the 0s) serially, one by one. To keep things simple, the 1s are going to be the HIGHs, and the 0s are going to be LOWS, and then these bits will come together to form some bytes. 

When you see a byte, it means that you have a sequence of 8 bits. Each bit that you work with will represent a number that you need to add. The first bit, or the one that is on the far right, will represent one place, kind of like what you are used to working with in the decimal system. 1 is going to equal 1 and 0 will equal 0. Then when you move to the value of the net bit, you would multiply it by two, so 10 is going to equal 2, 11 will equal 3 and 01 will equal 1 and so on. These bits can go all the way up to 255.

Serial output code

Now it is time to work on the code that you want to use in order to help with the serial output. This code is pretty simple, and you will just need to type in the following to make this happen:

 

void setup (){

Serial.begin(9600)

}

void loop(){

Serial.print(“The analog value is: “);

Serial.println(analogRead(A 0));

delay(100);

}

 

As you can see with this one, there is the setup() and the loop() function just like we did with the earlier examples that we showed. In addition, the serial print is going to be a function that is in charge of printing whatever message you specify in your serial monitor. For this specific code, we have decided to print the analog value for the point that is inside of Analog Pin 0.

Then we moved on to the void setup. This is where we will tell the serial monitor when it is supposed to start. For this project, it should start up when we reach the baud rate of 9600. During the void loop, there is the function for the Serial println as well. IN here, we are going to print out the value that we find in the A0 part of the Analog Read. 

How to Use it

So far, we have taken the time to look at a code for the serial communication and to explore what this type of communication is all about, but what is it all about? Why would we want to use this type of communication and how can be it used on the Arduino board? Over the years, serial communication has been used in many ways to help finish out the projects that you would like to work on.

In this project, we are going to turn on and off an LED light. This will be similar to what we did before, but we will use the serial numbers rather than the other method that we used before. First, you will need to take the LED and place it with the pin-13 and the ground. Then you can connect the USB cable that you want to use. 

We can then move on to create a new integer that we will use to store the incoming bytes that are sent over. You can pick the name that you want to call this integer, but we are going to call it “inByte” for this example. Now we will go back to the void setup, and make sure that we set the pin13 as the output. We will keep the Serial.begin the same since we want to get it to work with 9600bits per second.

Now it is time to move to the void loop. In this, we first need to know which byte is around that it can read. So we will use the Serial(available). This function will let you know how many bytes are there for you to read through. So, we can choose to write out something like “if(Serial.avialable() > 0)”, we know that there will be one or more byte that will show up that can be read. Inside of this if statement, we have to read the byte and then make sure that it is stored later for use. We can do this by writing out Serial.read(), which will tell the program to read your byte and then save it over to the inByte. To finalize this line, it is going to say inByte – Serial.read();

While you are still inside your main if statement, we can add in some other if statements if we like. If you want the program to write or read a letter to your variable, you will place that particular letter in some single quotation marks. For this example, we want to let the LED light turn on when we send over an ‘a’ to our boards. This means that the statement we will write to make this happen would include “if(inByte == ‘a’).

Next, we will move on to the Serial.printlin(). This is important because it is going to tell Arduino that it should notify us inside of the Serial monitor that the ‘a' has shown up and that our LED light has started. The if statement that we want to work with will be like the following:

 

if(inByte == ‘a’) { // byte is ‘a’

digitalWrite(13, HIGH);

Serial.println(“LED – ON”);

}

 

In this project, we need to ensure that the LED will turn off if we put in any of the other letters outside of the ‘a’ that we specified before, so it is important that we add in an else statement. When this happens, the LED light is going to turn itself off.

Putting it all Together

Up to this point, we have spent some time talking about the code that is needed to turn the light on and off based on the Serial communication, or the binary code. Now let’s put it all together. This will tell the program that you want to turn the light on when the ‘a’ is put in, and then when any other letter is added to the mix, the LED will be turned off. The code you need to make this happen includes:

 

int inByte; // Stores incoming command

 

void setup() {

Serial.begin(9600)

pinMode(13, OUTPUT); // Led pin

Serial.println(“Ready”); //Ready to receive commands

}

 

void loop() {

if(Serial.available() > 0) { // A byte is ready to receive

 

inByte = Serial.read();

if(inByte == ‘a’) { // bye is ‘a’

digitalWrite(13, HIGH);

Serial.println(“LED – On”)

}

Else { //byte isn’t ‘a’

digitalWrite(13, LOW);

Serial.printlin(“LED – off”);

}

}

}

 

And that is all that you need to do in order to get the Arduino board to work properly and use an LED light with the help of the Serial communication.

Назад: Chapter 6: USB and Serial Communication on Arduino
Дальше: Chapter 7: Can Arduino Work Wirelessly?