Digital Sandbox Experiment Guide

Contributors: jimblom, bri_huang
Favorited Favorite 8

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

Here is the layout for this experiment:

alt text

There are two new blocks this time, both of them shaded white and located under the communication bin:

  • Serial Print: This block takes two parameters. At the top, place the message you want to print. You can put anything you want into the message block (even spaces!). If you want to add a variable or number, you'll need to add some glue. The bottom snap of Serial Print determines if a new line is printed after the message. Usually you'll want this to be set to true.
  • Glue: Glue blocks allow you to print values like variables or numbers - anything that isn't a message you've written in. If you want to print a variable, you'll need to add a glue block between the variable and the Serial Print block. There are three different kinds of glue blocks, each with a different snap shape on the right. This time we'll use the block with a wedge (<) termination.

Do This

At the very beginning of our sketch, we want to print a friendly message. How about a short description of what our calculator is going to do: "Powers of 2!" Then setup a variable that we can do some math on, starting at one.

In the loop, we only want to do math and print when the button is pressed. So, begin by adding an if block to check if pin 12 is HIGH (button is pressed). If the button is pressed, we'll do our math and print out the result. To print a variable, we need to "glue" it to the Serial Print block with a wedge-shaped glue piece. Make your drawing match the one above.

With that, upload the sketch to your Sandbox. Then, to view the serial prints, click on the Serial Monitor button up top.

alt text

You'll see your message printed. Now press the D2 button to start calculating.

alt text

Further Explorations

  • Something funny happens when the power of 2 gets past 16834, and then turns to -32768, 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. You're probably familiar with +, −, ×, and ÷, but what does the % operator do?