Project 41 – Ultrasonic Theremin

For this project, you are going to use the same circuit. Although you won't be using the potentiometer, switch, or reset button in this project I am leaving them in to give you the flexibility to modify the project if you wish—plus it means you can jump back to using Project 40 if you wish later.

This time you are going to use the sensor to create a Theremin that uses the sensor ranging instead of the electrical field that a real Theremin uses. If you don't know what a Theremin is, look it up on Wikipedia. It is basically an electronic instrument that is played without touching it by placing your hands inside an electrical field and by moving your hands inside that field. The device senses changes in the field and plays a note that relates to the distance to the coil. It is difficult to explain, so check out some videos of it being used on YouTube. As the circuit is the same, I will jump right to the code.

Enter the Code

Enter the code in Listing 14-4.

Listing 14-4. Code for Project 41

// Project 41

#define sensorPin 9

#define lowerFreq 123 // C3
#define upperFreq 2093 // C7
#define playHeight 36

float pwmRange, inch, cm, note;

void setup() {
        pinMode(sensorPin, INPUT);
}

void loop() {
        pwmRange = pulseIn(sensorPin, HIGH);

        inch = pwmRange / 147;
        // convert inch to cm
        cm = inch * 2.54;

        // map the playHeight range to the upper and lower frequencies
        note = map(inch, 0, playHeight, lowerFreq, upperFreq);
        if (inch<playHeight) {tone(8, note); }
        else {noTone(8);}
}

Once you upload it to the Arduino, you can now enter your hand into the sensor's beam and it will play the note mapped to that height from the sensor. Move your hand up and down in the beam and the tones played will also move up and down the scale. You can adjust the upper and lower frequency ranges in the code if you wish.

Project 41 – Ultrasonic Theremin – Code Overview

This code is a stripped down version of Project 40 with some code to turn the sensor range into a tone to be played on the piezo sounder or speaker. You start off by defining the sensor pin as before.

#define sensorPin 9

Then you have some new definitions for the upper and lower notes to be played and the playHeight in inches. The playHeight is the range between the sensor and as far as your arm will reach while playing the instrument within. You can adjust this range to something more or less if you wish.

#define lowerFreq 123 // C3
#define upperFreq 2093 // C7
#define playHeight 36

The variables are declared with one for note, which will be the note played through the speaker:

float pwmRange, inch, cm, note;

The setup routine simply sets the sensor pin to be an input:

pinMode(sensorPin, INPUT);

In the main loop, the code is just the essentials. The value from the sensor is read and converted to inches:

pwmRange = pulseIn(sensorPin, HIGH);
inch = pwmRange / 147;

Next, the inch values from zero to the value stored in playHeight are mapped to the upper and lower frequencies defined at the start of the program:

note = map(inch, 0, playHeight, lowerFreq, upperFreq);

You only want the tone to play when your hand is inside the beam, so you check if the value from the sensor is less than or equal to the play height. If so, a hand must be within the play area, and therefore a tone is played.

if (inch<=playHeight) {tone(8, note); }

If the hand is not in the beam or removed from the beam the tone is stopped:

else {noTone(8);}

Play around with the playHeight, upperFreq, and lowerFreq values to get the sound you want.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.106.204