Digital Sandbox Arduino Companion

Pages
Contributors: jimblom
Favorited Favorite 3

9. Serial Calculator

While you probably can't have a very stimulating conversation with the Digital Sandbox, it can send you some very interesting information. It's great at math, so let's have the Sandbox do some calculating for us! Trouble is, how do we get it to print numbers (without Morse code)? Enter serial communication!

Background Information

Serial communication is a form of data transmission where we can send a string of 1's and 0's between two devices and actually form a set of characters. So 01101000 01100101 01101100 01101100 01101111 00101100 00100000 01110111 01101111 01110010 01101100 01100100 becomes "Hello, world."

With serial, we can send actual text from the Sandbox and display it on our computer using the Serial Monitor.

Active Parts

alt text

Code Components

This sketch introduces a pair of functions related to Serial communication. Plus, a new data type!

Serial Communication

In order to use serial communication, you first have to initialize it by calling Serial.begin([speed]). The begin function requires one parameter -- the baud rate -- which controls how fast data is transmitted. The baud rate is usually selected among a set of common speeds: 4800, 9600, 19200, 38400, 57600, or 115200. For example, to initialize Serial at 9600 baud, this line of code will do:

language:c
Serial.begin(9600);

9600 is a good, default speed for most applications. We'll use it throughout our experiments. You'll usually want to stick your Serial.begin() statement in the setup() function. Call it once, set it, and forget it.

After Serial has been initialized, you can start printing messages using either Serial.print([message]) and Serial.println([message]). Both functions work to print a message, but the Serial.println() function adds a new line after the message.

You can print all sorts of with Serial. You can print static, pre-coded messages, by surrounding them with quotes ("), like this:

language:c
Serial.print("This is a long message, which you can print with Serial!");

Or you can print a variable, like this:

language:c
Serial.println(example_variable);   // Print the value of example_variable

As you continue to explore with Serial, you'll find it's a powerful debugging tool. It gives you a window into the Sandbox's mind -- you can print out the values of your variables.

Long Data Type

We'll be using a new variable data type in this experiment: long. A long variable stores 65536-times more values the the int! It can store numbers in the range of -2,147,483,648 to 2,147,483,647. Billions!

Why not always use long? Well it uses twice as much memory -- 32-bits. The Sandbox doesn't have unlimited memory, so it's best to conserve it when you can.

Sketch and Experiment

Here's our Sandbox_09_Serial_Calculator.ino sketch. Check out the comments for a line-by-line overview.

language:c
    // Sandbox 09: Serial Calculator

/* While you probably can't have a very stimulating conversation with the 
   Digital Sandbox, it can send you some very interesting information. It's 
   great at math, so let's have the Sandbox do some calculating for us! Trouble 
   is, how do we get it to print numbers (without Morse code)? Enter serial 
   communication!

   This experiment introduces Serial Communication. New functions include
   Serial.begin() and Serial.println(). Serial is a form of data transmission 
   where we can send a string of 1's and 0's between two devices and actually 
   form a set of characters. So 01101000 01100101 01101100 01101100 01101111 
   00101100 00100000 01110111 01101111 01110010 01101100 01100100 becomes 
   "Hello, world."
*/  

// Let's create a base value to do math on. This time, we'll use the "long"
// variable type -- a 32-bit variable, which can be anywhere from 
// -2,147,483,648 to 2,147,483,647.
long multiplier = 1;

void setup()
{
    // To initialize serial communication, we need to use the Serial.begin()
    // function. The number inside the function determines the baud rate -- the
    // communication speed. 9600 is a very standard, default speed. Good for
    // most debugging.
    Serial.begin(9600);

    // Serial.println() is used to print a message out to the serial monitor
    // you can print variables, or static text defined inside quotes ("").
    Serial.println("Powers of 2!");
}

void loop()
{
    if (digitalRead(12) == HIGH)    // If the button is pressed
    {
        // On top of printing static messages (embedded inside quotes), we can
        // also print the value of variables.
        Serial.println(multiplier);

        // Multiply the multiplier variable by 2. To multiply we use the 
        // asterisk (*) character.
        multiplier = multiplier * 2;

        delay(250); // Short delay for visibility on the serial monitor.
    }
}

After uploading the sketch, if you're waiting for the Sandbox to do something visual, you'll be waiting forever. Instead open the serial monitor. Click the Serial Monitor button up in the top right corner of the Arduino IDE.

After opening the Serial Monitor, press the D12 button. You should see multiples of 2 start to zoom by. Now you're calculating!

Your Turn!

  • Something funny happens when the power of 2 gets past 1073741824, and then turns to -2147483648, and then turns to zero. This is because our variable has reached its maximum value and, in a sense, has gotten confused. Can you add an if statement to catch an out-of-bounds multiplier variable and reset it?
  • Try some of the other mathematical operators. Add with +, subtract with -, or divide with /. Can you figure out what the % (modulo) operator does?