7
The Research Experience for Teachers Program http:// www.cs.appstate.edu/ret Activity Title: Creating a Music Box with Arduino and Littlebits Summary: Little Bits are small, interactive pieces that fit and work together to create electronic inventions. Using and Arduino, a microcontroller, we will create a Music Box, or a contraption that will play a tone depending on the color it reads with its light sensor. Introduction/Motivation: Littlebits and an Arduino can be combined to create surprisingly sophisticated projects. This lab serves to challenge logical thinking, ingenuity, and programming skills. Using a set of Littlebits, including a light sensor and a speaker, we will develop an invention that will read a color off a page, set it equal to a note on a scale, and then “read” a song using multiple colors and playing their respective tone. Pre-Requisite Knowledge: Intermediate C++ and Arduino Programming ability Materials List: Two Micro-USB to USB adapters (should come with the Arduino and Littlebits kit) Littlebits w6 Arduino Module Littlebits p3 USB Power Module Two Littlebits o3 RBG LED light Module (or any other light module) 3 Littlebits w1 Wire Module 1 w7 Fork Module i3 Button Littlebits Module Littlebits Speaker PC, Linux Machine, or Mac Desktop that allows for download and installation of software Procedure: This lab consists of building a reasonably complex creation, and learning how C++, Littlebits, Arduinos, and some light mechanics work along the way. Background:

cs.appstate.edu · Web viewIf you downloaded the Windows Zip file for non-admin install, first the zip file must be expanded. Search your directory for the Arduino windows zip file

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: cs.appstate.edu · Web viewIf you downloaded the Windows Zip file for non-admin install, first the zip file must be expanded. Search your directory for the Arduino windows zip file

The Research Experience for Teachers Program http://www.cs.appstate.edu/retActivity Title: Creating a Music Box with Arduino and Littlebits

Summary: Little Bits are small, interactive pieces that fit and work together to create electronic inventions. Using and Arduino, a microcontroller, we will create a Music Box, or a contraption that will play a tone depending on the color it reads with its light sensor.

Introduction/Motivation: Littlebits and an Arduino can be combined to create surprisingly sophisticated projects. This lab serves to challenge logical thinking, ingenuity, and programming skills. Using a set of Littlebits, including a light sensor and a speaker, we will develop an invention that will read a color off a page, set it equal to a note on a scale, and then “read” a song using multiple colors and playing their respective tone.

Pre-Requisite Knowledge: Intermediate C++ and Arduino Programming ability

Materials List:

Two Micro-USB to USB adapters (should come with the Arduino and Littlebits kit) Littlebits w6 Arduino Module Littlebits p3 USB Power Module Two Littlebits o3 RBG LED light Module (or any other light module) 3 Littlebits w1 Wire Module 1 w7 Fork Module i3 Button Littlebits Module Littlebits Speaker PC, Linux Machine, or Mac Desktop that allows for download and installation of software

Procedure:

This lab consists of building a reasonably complex creation, and learning how C++, Littlebits, Arduinos, and some light mechanics work along the way.

Background:

You should understand the basics of installing and updating the proper software on the computer that you plan to use. Groups may ask each other for help or advice, but each team must set up their own Littlebits Arduino.

Preparation:

Make sure that Java is updated to the latest version, or at least Java v7. If working on Mac OS X, update to 10.7 Lion or newer. For Linux, please learn whether your computer is 32 bits or 64 bits. (If not sure, you should use 32 bits in the lab) No preparation is necessary for Windows machines. The directions below are specific to the Windows machine.

Page 2: cs.appstate.edu · Web viewIf you downloaded the Windows Zip file for non-admin install, first the zip file must be expanded. Search your directory for the Arduino windows zip file

Assembling the Music Box:

1. Connect the power module to the small end of the w7 Fork Module.2. From the top to the bottom of the Fork, connect an RGB LED, a Light Sensor, and another RGB LED. They

should all be connected to their own separate extension of the fork.3. To the Light Sensor and the top RGB LED, connect two wires, one for each Bit.4. To the other end of the wire connected to the RGB, connect the i3 Button Bit.5. Attach this entire segment (RGB, Wire, Button) to the D0 port of the Arduino (Top left)6. Connect the wire leading from the Light Sensor to the A0 port (Middle Left, directly under the Button)7. On the opposite side of the Arduino, connect the o24 Synth Speaker to the D1 Port (Top Right)

You’re done assembling the Music Box! Now, let’s take a look at the code that will make this work. But first, let’s download the software that we’ll need.

1. Navigate to https://www.arduino.cc/en/Main/Software 2. Select the download type for your computer. This will open a donation page, but you do not need to

donate to proceed. Simply hit the Just Download button in grey on the left side of your screen. This should begin the download of the Arduino IDE. Remember which directory you are saving this download in, as we will need to come back to this download.

3. Once the download has completed, open the Arduino IDE software. If you downloaded the Windows Installer, simply accept the Terms of Agreement and allow for changes to the computer and you may skip to Step #6 below. For other installations, see below, as further downloading may be required.

4. If you downloaded the Windows Zip file for non-admin install, first the zip file must be expanded. Search your directory for the Arduino windows zip file (arduino-1.6.9-windows.zip), select the file, then left click and select “Extract All…” This should open a pop-up indicating where you wish to store the new file. Select where you wish to store the file, but make sure you take note of where it is. This new file will contain all of the materials for your Littlebits Arduino.

5. Then you must search your new file for a folder called “drivers”, and select the file “arduino.inf” within your “drivers” folder. Left-click this selected folder and choose “Install” from the options. This should finish installation.

6. Attach the remaining micro USB to USB adapter to the micro USB port on your Arduino, and the USB port on your computer.

Page 3: cs.appstate.edu · Web viewIf you downloaded the Windows Zip file for non-admin install, first the zip file must be expanded. Search your directory for the Arduino windows zip file

7. Go back to the Arduino folder and open the file labeled “arduino.exe”. Once this has successfully started, go to the “Tools” pull down, which is in the top right of the window. From this, go to “Board:”, and select “Board Manager.” In the field, search “Littlebits”, and select to install the option that appears.

8. Next, go back to “Tools.” Go to “Port,” and verify the port is connected to Arduino Leonardo (usually listed in parenthesis).

That’s all for setup! Let’s get coding! Go ahead and open up your Arduino IDE if you haven’t already, and we can start typing some stuff down.

Theory: This is how the Music Box should work, in theory:

1) By placing the light sensor over a color, you should be able to press a button and “set” the color to a tone in the scale.

2) This should repeat 8 times, for each note in the scale.3) After all 8 colors are matched with a note in the scale, you should be able to hold the light

sensor over the color again and hear the note. This should continue until you turn the Arduino off.

So this is, generally, what we want our code to do. Let’s take a look at the individual parts.

Fields: Fields, in computer science, refer to the variables and constants that we want to use in the program. What kind of fields will we need?

An array to hold the values read by the light sensor (8 indices large) A second array to hold the values for the notes we want to read (We will provide this, but you

can change it if you want!)

Some optional fields can also be included. For your inputs and outputs, you can use variables to represent the pins. This might look like:

const int buttonPin = 0;

That sums up all the fields we’ll use in this program. Let’s take a look at the setup now:

Setup: In the setup, we want to do two things:

1) Initialize the input/output pins. 2) Fill our lightValues array, which you declared in the Fields section.

Initializing the input/output pins isn’t too hard. They’ll look something like this:

pinMode(buttonPin, INPUT);Just repeat that for all the inputs and outputs that you want to change. ButtonPin refers to the pin that the button is in, and in the Fields section, we set buttonPin equal to its pin (0).

Page 4: cs.appstate.edu · Web viewIf you downloaded the Windows Zip file for non-admin install, first the zip file must be expanded. Search your directory for the Arduino windows zip file

The other part of the Setup is to fill our array with values. Since we’ll be repeating the same steps (reading a color value, place it into the array), the best way to do this is to use a loop.

int i = 0;while (i < 10) {}This is what the loop should look like. We’re looping, in our case, 10 times: once for each color. However, depending on how many colors you want to read, it could be more or less times (But you probably don’t want to increase the number of colors. The light sensor can only be so accurate).

But what will be within the loop? Let’s create a list of what we need:

Read and Check if the button is pressedo If the button is pressed we need to:

Read the light value (which represents the color) and store it in the array Increment our variable i (which keeps track of what color we’re setting up & what note

we’re playing). Play a note (depending on how many notes we’ve read so far (which is represented by i))

This might seem a bit confusing. Let’s put this into a better structure. But you’ll have to write the code yourself!

int i = 0;int buttonState = 0; //This simply represents what state our button is in (up or down)while (i < 10) {

buttonState = //the current button state. Hint: use digitalRead()delay(100); //This helps the music flow//TODO: Play the tone found at the variable i in our tone array.if (the button is pressed) {

//TODO: Put the value read by the light sensor into our lightValue array at index i

//TODO: increment inoTone(speakerPin);delay(100);

}}

And that’s all for the setup! Onwards to the body of the code! (also known as loop)

So what does this method need to do? Let’s make another list:

Page 5: cs.appstate.edu · Web viewIf you downloaded the Windows Zip file for non-admin install, first the zip file must be expanded. Search your directory for the Arduino windows zip file

Store the value read by the light sensor Cross-reference this value with the your lightRead array.

o One important idea to consider: since the light sensor is very precise, it is easy to get a range of values when reading the color you want to read. In order to take this into account, we’ll want to allow for some “padding” around the light value read. This will be demonstrated shortly

o Once we find a “match” within the color array, we’ll want to play the corresponding tone found within the “tone” array.

And that’s all! Not to bad, right? Let’s put this into pseudocode format.

int light = //TODO: Read the light value. Hint: Use analogRead().delay(50); //Allows for smooth reading.for (the number of colors read into your array){

if (lightRead[i] + 20 > light && lightRead[i] – 20 < light) //This allows for a buffer of 20 on

//both sides{

//TODO: play the tone at i (which is where you found the specific light value.

}}delay(100);

And that’s everything! Refer to troubleshooting below if you have problems, or, better yet, practice your debugging skills! You know what the program should do, so make sure it all works right.

Troubleshooting: Pay attention to any error messages that appear in the console at the bottom of the Arduino IDE window. Common mistakes are:

Not selecting Arduino Leonardo as the Board (click on Tools) Not selecting Arduino Leonardo as the Port (click on Tools) Introducing a syntax error to the software when changing pin 13 to pin 9 Not having Java installed on the machine or having the wrong version of Java

Page 6: cs.appstate.edu · Web viewIf you downloaded the Windows Zip file for non-admin install, first the zip file must be expanded. Search your directory for the Arduino windows zip file