Intermediate Series Lesson 1: Serial Monitor and Pin Initialization
For our first Intermediate Series lesson, we’ll take a look at how to use the Serial Monitor and how to initialize our pins

Scene_08

Summary

The Serial Monitor is a useful tool built into the Arduino IDE.  Using the Serial Monitor, we will learn useful methods for debugging our code and testing our sensors.  We’ll also take a look at how to initialize our pins using the pinMode() function.

 

Introduction to the Serial Monitor

The Serial Monitor is a tool within Arduino that monitors the serial data to and from the selected port.  Serial is just a method of data transfer that involves sending bits one at a time sequentially.

To use the Serial Monitor, follow these steps

  1. Your CoDrone Controller is connected to your computer
  2. You have properly selected the Port (Tools > Port)
  3. Click the magnifying glass icon in the top right of the Arduino interface Serial

Serial Monitor

Serial Monitor

This opens the Serial Monitor display window.

Normally, your Serial Monitor won’t show anything.  To display data to the Serial Monitor we use the Serial Library.

 

Using the Serial Library

 

The Serial library is a powerful function library that is automatically included in the standard Arduino library.   There are 3 main functions that we will use from the Serial library:

Serial.begin();

Serial.begin(baud rate);

  • Sets the data rate is bits per second (baud rate) for serial data transmission. To properly observe serial data, the monitor’s data rate must match the baud rate set here.

Serial.print();

Serial.print(variable);

  • Prints the value of the variable to the serial monitor.

Serial.print(“string“);

  • Prints the given string to the serial monitor.

Serial.println();

Serial.println(variable/string);

  • Prints the value of the variable to the serial monitor then moves to the next line down.

Serial.println(“string“);

  • Prints the given string to the serial monitor then moves to the next line down.

 

Here are some examples

Example 1:

Output:

serial-monitor

Notice that our baud rate for the serial monitor matches the baud rate of Serial.begin(9600) .

serial-monitor-baud

If these don’t match, you’ll end up with gibberish on the monitor.

 

Example 2:

Output:

sm-2

This lets us clearly observe the progression of our variable x .

 

Using the Serial Monitor with CoDrone

Unfortunately, you can’t use the Serial Monitor with the CoDrone library.  Specifically CoDrone.begin() .

This is because the CoDrone library uses the SmartInventor Board’s serial port to communicate with the CoDrone’s bluetooth module.  If you try to use Serial functions in your CoDrone code, you will also be sending random bytes to the bluetooth module.  This will corrupt any data that the bluetooth is receiving, rendering the drone useless.

IMG_1454

Even though we can’t use the Serial Monitor with the CoDrone, it is still a powerful tool that we should be using often.  The best way to use it is as a testing ground for your program flow mechanisms.

For example, if I wanted to create a program that made my CoDrone sway left and right, I would first test my mechanisms with the Serial Monitor.

To sway left and right, my value for ROLL would have to go from positive to negative to positive ect…  (We did this before in the beginners series lesson on Flags)

Sweep Flag

If we want our number to bounce between -70 to 70, incrementing by 5 each time, we would write

With our Serial.println(Roll)  line, we can observe the value of the variable Roll  in the Monitor

sm-flag

We can see that our number works as intended.  We can now easily translate this to a CoDrone code with confidence.

Obviously this code won’t work (the CoDrone never leaves the ground), but it is representative of how easy it is to translate Serial code to CoDrone code.

 

Using Pins/Sensors with the Serial Monitor

The last thing we’ll learn about here is how to initialize pins.

Pins are specific wires that can be written to or read from.   For the SmartInventor Board, we have pins for the digital and analog sensors, the on-board LEDs, the buzzer, and everything else that is connected to the board including the CoDrone joysticks.

Digital IR Sensors Pins

You probably remember from our lesson on digital IR sensors the code to read a sensor

What you might not have realized is that there are two steps to using a pin:

  1. Initialization
  2. Read/Write

When using the CoDrone Library, the initialization of all used pins is already included in the header when you use #include <CoDrone.h> .  This is why you’ve never encountered a pin initialization before.

Since we can’t use the CoDrone library with Serial, we will learn how to initialize our own pins.

 

pinMode();

pinMode(pin number, mode);

  • pin number – The number of the pin you want to initialize
  • mode – The mode that you will use this pin in.  This will usually be INPUT or OUTPUT.

The difference between INPUT and OUTPUT is simple.

INPUT is something that gives your program a value/number.  A digital IR sensor is an INPUT device because it returns a 1 or 0 depending on whether or not it is covered.

OUTPUT is something that does not give your program a value/number.  An LED light is an OUTPUT device because it just turns on or off.  It receives commands, but does not send any.

Since pin modes are usually consistent, pin initializations usually go in the setup portion of your code.

Here’s an example of a code that will read and print the value of our digital IR sensor on pin 18

Example 3:

This is also a great way to test if all of your sensors are working!

 

Activity: Check Me Out

Screen-Shot-2016-03-17-at-8.26.46-PM

Use the Serial Monitor to test all of your IR sensors.  In a single program, initialize the 3 Analog and 7 digital IR sensors.  Then read and print the values to the Monitor.

Your output should look something like this

sm-sensor-check