This in-class lab is designed to help you get some experience with the ultrasonic sensors, and to build a musical instrument called a theremin.

Part 1: Getting started with the ultrasonic distance sensor

This page has some instructions on how to connect the ultrasonic sensors to the Arduino, and how to write code that uses them:

http://arduino-info.wikispaces.com/UltraSonicDistance

You'll need to use the NewPing library in order to make the ultrasonic sensors work. There are a few different versions out there; make sure that you use the version that is downloaded from our Moodle page. You'll find it in the first section of our course Moodle page, via a link titled "NewPing library: specialized version without timer conflicts". To install it, first follow the link to the NewPing library, which results in a zip file that you should download somewhere to your computer.  Then, in the Arduino software, from the menu bar, select "Sketch -> Include Library -> Add .ZIP library." This will allow you to select the NewPing zip file that you downloaded.

When plugging in the ultrasonic, connect the VCC pin to any of the voltage pins on the Redback, the GND pin to any ground pin on the Redback, and then choose two digital pins: one for the Trig pin, and one for the Echo pin. You should then be able to use the sample code provided on the above-linked web page.

You should hopefully be able to get the ultrasonic sensor measuring distance to your hand or a book, and printing those distances to the serial monitor.

One challenge that sometimes happens is that the ultrasonic sensor can be noisy. It will sometimes sporadically show a nonsense value. One way to resolve this is to read five values in a row, then take the average (or even better, the median, though that is harder to compute). If you are seeing some noisy values, you might try to do this. If you go this route, use a "for" loop to read a series of values at once and average them. If you want to calculate a median, you'll need to store them in an array, and then sort them. Ask for help if you're interested in going this route, if you need it.


Part 2: Building a theremin

A theremin is a musical instrument which measures positions of your hands in space: one hand controls the frequency, the other controls the volume. Here are two different videos of a theremin being used:

https://www.youtube.com/watch?v=K6KbEnGnymk

https://www.youtube.com/watch?v=4kujH0ScAi0

Build an Arduino theremin, using two ultrasonic sensors and the toneAC library. We're using the toneAC library because it allows you to control volume. (toneAC allows you to use two pins for the speaker if you wish in order to get extra volume, but extra volume isn't something we really need. One pin is enough.) toneAC does restrict pin usage, you'll need to plug the speaker into pin 11 or 12. To install the toneAC library, visit this page:

https://bitbucket.org/teckel12/arduino-toneac/wiki/Home

There's a link partway down the page for downloading the library. To install it, follow the same instructions above as for installing the NewPing library.

Connect a beeper to the Arduino as we always have; choose pin 11 or 12, it doesn't matter which one.

The webpage describes how to use toneAC, as well as some sample code. Try out some code first where you change the volume with code you write.

If you can make the above work, connect two ultrasonics, where one should be used to control frequency, and the other to control volume. Do your best to arrange the two ultrasonic sensors to have some distance and point away from each other so they don't interfere.

Ask for help if you need it!