Greg P Final Project

Greg Prusiewicz

Tone Modification


My project is a variable resistor hooked up to Arduino which will change the pitch of a tone played through a piezo.


This is a modification of a play melody program taken from the Arduino website. That program establishes the frequencies of notes, sets up a predetermined melody and then plays back the melody. I modified the code so that the melody is based upon an analogRead from the potentiometer. Therefore, I can control the frequency of the note played with the potentiometer.

/* Play Melody

* -----------


* Program to play a simple melody


* Tones are created by quickly pulsing a speaker on and off

* using PWM, to create signature frequencies.


* Each note has a frequency, created by varying the period of

* vibration, measured in microseconds. We'll use pulse-width

* modulation (PWM) to create that vibration.

* We calculate the pulse-width to be half the period; we pulse

* the speaker HIGH for 'pulse-width' microseconds, then LOW

* for 'pulse-width' microseconds.

* This pulsing creates a vibration of the desired frequency.


* (cleft) 2005 D. Cuartielles for K3

* Refactoring and comments 2006

* See NOTES in comments at end for possible improvements


int potpin = 2;

// Set up speaker on a PWM pin

int speakerOut = 9;

void setup() {

pinMode(speakerOut, OUTPUT);

pinMode(potpin, INPUT);


// melody[] is an array of notes, accompanied by beats[],

// which sets each note's relative length (higher #, longer note)

int melody[] = {analogRead(potpin) };

int beats[] = { 4};

int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo

long tempo = 10000;

// Set length of pause between notes

int pause = 1000;

// Loop variable to increase Rest length

int rest_count = 100;

// Initialize core variables

int tone = 0;

int beat = 0;

long duration = 0;

// Pulse the speaker to play a tone for a particular duration

void playTone() {

long elapsed_time = 0;

if (tone > 0) { // if this isn't a Rest beat, while the tone has

// played less long than 'duration', pulse speaker HIGH and LOW

while (elapsed_time < duration) {


delayMicroseconds(tone / 2);


digitalWrite(speakerOut, LOW);

delayMicroseconds(tone / 2);

// Keep track of how long we pulsed

elapsed_time += (tone);



else { // Rest beat; loop times delay

for (int j = 0; j < rest_count; j++) {





void loop() {

// Set up a counter to pull from melody[] and beats[]

for (int i=0; i<MAX_COUNT; i++) {

tone = analogRead(potpin+2000);

beat = beats[i];

duration = beat * tempo; // Set up timing


// A pause between notes...




Circuit Diagram:


I had an extreme amount of problems with my project. The original idea began as making a drum machine/keyboard using piezos and a MIDI controller. However, after a few weeks of research, I found that the MIDI codex was too complex and so I scrapped that idea. Then, I began working on a Theremin type device. However, I wasn’t able to get Arduino to play a note based upon a number so I moved on to the bulk of where I spent my time: Processing. Mr. Dickie introduced me to Processing and I was able to find a library that dealt with mp3 modification. My idea then was to make a looping machine where I could play mp3s and then modify their tempo, pitch, rate, and reverse them. There was no code to base this idea on so I had to invent my own. After learning how to get Processing to serial read a button press from Arduino and how to get Processing to play an mp3 and then multiple mp3s based upon a button press, I ran into serious problems. I was unable to get Processing to read a stream of information, such as an analog read or multiple buttons. Without this capability, my looping machine would not work. With very little time left, Mr. Dickie introduced me to the playmelody code and I was able to modify it to make a Theremin-like machine.

What I would like to add:

I would like to return to the Processing code and learn how to make that work. I just did not have the time/ability to learn enough Processing to make that work. I thought that it was a really cool idea and I’m sure that there is a way to make it work, but I just could not figure it out. With this project, if I had more money I would like to turn it into an actual Theremin. There are projects online to do so, but they cost more money that I was willing to spend.