Sunday, February 15, 2015

#7 - Advanced Arduino: Making Music, Using the Serial Port, and Toy-Hacking

Advanced Programming

We've been able to play around with the Arduino for a while, and have learned how to execute some basic functions. Now we will learn some new, more advanced functions, such as: using arrays, creating/accessing libraries, and using the serial port. Later, we will use some of our learned knowledge to hack into a toy, and have our Arduino control portions of its operation. But first...

Making Music

We will be using a piezo sounder (that we used previously as a buzzer) to assist us in learning how to use arrays, and how to create/access libraries.

The Piezo Sounder As A Theramin

We've learned how to setup the sounder in the previous labs, and now, we will practice our programming skills and create a makeshift theramin. For those who don't know, a Theramin is a type of electronic musical instrument, that creates music based off of sensors that detect the spacing and the movement of your hands. What we will use as our sensor is the LDR (light-dependent-resistor), and will program our Arduino as a Theramin that works based off of the apparent light that the LDR detects. Let's try it out!
The circuit setup for the LDR.

Here we see the LDR Theramin in action. As I move my hand further and closer away (thus providing more or less light), the frequency emitted by the sounder will change, thus creating a different pitch of sound.

Using Arrays and Libraries

Now, we will delve even deeper. To play music, we must learn how to use arrays and external libraries. So, first we must define these terms. An array is a user-created set of variables that can be accessed using an index number. There are a few ways to create an array, some of which include these:
int myInts [6];   //Declares an arrays without initializing it as int myInts.
int myPins[] = {1, 3, 2, 5, 7};   //Declares an array without specifically choosing a size.
*Don't forget the semicolon!

To access the array, just enter myPins[somenumber 1 less than the maximum number of entries in the array, since arrays are zero-indexed (start counting at 0)].

To access outside libraries, we will use #include to include outside libraries (which will have been created previously, labeled as a filename.h type function).
Now, to use these to create music! There is  pitches.h file already created on the arduino website. Copy and paste it, access it and add it to your compiler/code! Use arrays to label and choose which notes are played and for how long. Then, use LEDs to light up in sync with the music. This is demonstrated below.

Using The Serial Port

Another important tool to learn to use is the Serial Monitor/Serial Port. The Serial Monitor is a window that relays and debugs information. To setup the serial monitor, use the following code:
Serial.begin(9600);   //This initializes the serial port with a BAUD rate of 9600. Baud refers to the number of bits transferred per second over the serial communications line.
To have the serial monitor return information back to you, use variations of the following code:
Serial.print(sensorValue);   or   Serial.println(sensorValue);    //These tell the serial monitor to type and send the values read from the sensor into the monitor. The 'ln' at the end of print returns the displayed characters on a new line of data (much like pressing Enter in a word processor).
Now that we know how to use it, we can program this serial port/monitor to turn on or turn off an LED whenever we type in either a "0" for LOW/off, or a "1" for HIGH/on.
Typing "0" in the serial monitor turns off the LED...

While typing "1" turns the LED on.
Example of the code used.

Hack-a-Toy

Using the skills we've practiced, now it is time to see whether or not we can get a pre-programmed toy to do certain actions that we want it to do. Let's begin!

First, remove the batteries so that we can gain access to the screws.

We had to cut off some of the skin in order to access all of the screws.


Here is how the inside of the toy works. It contains motors to dance, and buzzers/sounders to play music.

For now, we will try to access the motors and tell it to activate upon our Arduino's request.
Here we see the toy moving due to the Arduino choosing when to supply power.

Putting the cover back on.

And here we see the monkey dancing according to our program! By using a PWM signal, we can alternate when the motors will activate, and where they will spin.

No comments:

Post a Comment