Beginners Series Lesson 2: Pairing
In this lesson we define a function and discuss the different methods of pairing with the CoDrone

Before we move on, let’s define a function.

What is a function?

A function is a set of code that is saved and given a name.  For example, if I wanted to make a sandwich, I would say the following

Now, if I wanted to make 5 sandwiches, I would have to say

This is a pretty long order.  To shorten this, we can just save the commands to make a single sandwich and name it “make a sandwich”.

So for 5 sandwiches

Much easier!

This is pretty much how functions work in programming.

(This isn’t really the code for AutoConnect(), just a simplification)

Functions can be identified as any code with parenthesis after it.

Some examples:

• CoDrone.begin(115200)
• setup()
• x = millis()
• delay(5000)

You’ll realize soon that functions are almost everywhere in programming!  In later lessons, we’ll learn how to write and use our own functions.

## AutoConnect

Now that we know what a function is, let’s move on to pairing with our CoDrone.  In our previous lesson, we paired to the CoDrone using the function CoDrone.AutoConnect(NearbyDrone)

This function pairs the controller with a nearby CoDrone.  The Nearby command is the easiest to use of the pairing commands and will probably be enough for most of your CoDrone needs.  Some other ways to use the CoDrone.AutoConnect()  function are shown below.

CoDrone.AutoConnect();

CoDrone.AutoConnect(NearbyDrone);

• Automatically connect to a nearby CoDrone.
• The code will not move past this function until a connection has been found.

CoDrone.AutoConnect(ConnectedDrone);

• Automatically connect to the previously connected CoDrone.
• The code will not move past this function until a connection has been found.

• Automatically connect to the specified CoDrone.
• The code will not move past this function until a connection has been found.

Code to connect to a nearby drone:

This command will automatically pair with a nearby CoDrone. This is the easiest way and will work in most cases.

The command CoDrone.AutoConnect(Nearby)  does not connect to the nearest drone.  It will connect to a random CoDrone within range.  If you are connecting near a lot of CoDrones, it’s best to connect one at a time to avoid confusion.

Code to connect to a previously connected drone:

This method will connect to the last CoDrone you were previously paired with.  Use this method if you are in an area with multiple CoDrones and accidentally disconnect from yours.

Code to connect to a drone with an address:

Each CoDrone has a unique address given to them.  By saving this address as a byte ( byte droneAddress[6] ), you can connect to the specific drone of your choice.  This method is great if you find yourself surrounded by CoDrone users.

Find the address of a drone:

Note that for this program you need to first use one of the other options for connecting to the CoDrone.

1. Upload this code to the controller but keep the controller connected to the computer via upload cable.
2. Turn the controller on and open the serial monitor (magnifying class in the top right corner).
3. The baud rate must be at 115200 (bottom right of the serial monitor window.)
4. Make sure the controller is no longer in upload mode.  Hit the reset button.
5. Your CoDrone’s unique address will print on the serial monitor.  Write it down and save it somewhere for later use.

Pairing Indicators:

When the CoDrone is pairing, the green light on the back of the drone will blink.  When it has found a controller to pair to, it will become solid green:

In the same way, the bluetooth board will blink red while looking for a drone to pair, and the blue LED lights on the SmartInventor board will repeat an inward moving pattern.  When it has found a CoDrone to pair to, the bluetooth light will become green and the SmartInventor board will stop the pattern:

Any of these methods will pair to your CoDrone. Once the connection is established, there is no difference in the functionality or capabilities of each method.  Use the one that you suits you best