Dead Man’s Music Box

The instrument I created for this assignment is the first step towards a bigger project that I’m eager to make (if I can), which I have been thinking of for a long time.

The Inspiration

This is Davy Jones’s heart inside the Dead Man’s Chest.

The image, as well as the character, come from Walt Disney Pictures’ 2006 film Pirates of the Caribbean: Dead Man’s Chest. The following is the Wikipedia entry for Davy Jones, a clear explaination of who he is and why his heart is kept in a chest:

In the story, his original purpose was to carry souls of those who died at sea from this world to the next on his vessel. He was charged with this noble and just duty by his one true love, the sea goddess Calypso. For every ten years at sea he could spend one day on land to be with her. Ten years after first being charged with the duty, Jones prepared to be reunited with his true love, but was heartbroken and enraged when she did not appear. Unable to deal with the sadness of Calypso’s betrayal, Jones, through some unknown supernatural means, ripped out his heart and locked it in a chest, burying it…

I’m writing about this old guy’s heart because I really, really really, really really really like “Davy Jones”, the movie’s character theme composed by Hanz Zimmer.

Inspired by Pirates of the Caribbean and Zimmer, I wanted to create a music box that plays the first portion of this piece, presented as if it were the Dead Man’s Chest (therefore, with a “beating” heart inside it).

The Circuit and Code (or Music and Lights)

The main feature of this project is that the music and the lights/colors occur together, the latter following the former’s structure. Thus, the music and lights help create the image of the heart’s beat.

To code the music, which is produced by piezo buzzers in the circuit, I used the piano sheet music for “Davy Jones”. This gave me not only the notes, but also the duration of each. It also provided me with the opportunity to make the music box more complex: instead of just the “main” melody (I apologize for ignoring a better term), written on the upper staff, I could also incorporate the notes from the lower staff. Thus, my music box could play both the right-hand and the left-hand piano notes for this piece.

This came with its own issues, mainly because my code loops through the notes in each staff to carry out its functions. Given that the staves’ notes don’t correspond to each other, in terms of quantity and durations, looping through both was impossible. I attempted to loop through the measures in the staves, which are equal in both cases, but this proved to be overly complicated for me.

I solved the issue using two RedBoards, thus running two codes, one per piezo buzzer. In doing this, I learned that the two circuits need a common ground (meaning, the grounds of both have to be connected), just as with circuits that are controlled by the same RedBoard. Failing to notice this at first, the readings of my analog sensors were greatly altered, and weird stuff was happening. But allas, it was fixed (shout-out to James!).

Circled in red: right piezo buzzer. Circled in light green: left piezo buzzer. Circled in purple: RGB LEDs. You have to squint your eyes to see the circles. Sorry ’bout that.

In terms of light, I programmed two RGB LEDs so that they change color according to changes in the notes. They both have red as the base color (meaning red’s value is 255, while green and blue are 0). However, the “left-hand” LED’s green values change, as well as the “right-hand” LED’s blue values. These alterations follow the increments and reductions of frequency in the sound (changes of notes), such that a higher frequency leads to a greater value for green and blue, and a lower frequency to a lower color value (it’s more noticeable than in the following video, my phone is not too great for LED recording:/).

These are the codes, written after Tom Igoe’s Arduino tone() Tutorial (they both mention the use of a potentiometer and a switch, which I’ll explain next):

Right:

#include "pitches.h"

const int knob = A0;
//potentiometer
const int power = 8;
//swtich

const int red = 3;
const int green = 5;
const int blue = 6;
//LED

int right[] = {
 NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_C5, NOTE_AS4, NOTE_A4,

NOTE_A4, NOTE_AS4, NOTE_C5, NOTE_D5, NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_A4,

NOTE_AS4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_AS4, NOTE_A4, NOTE_F4, 
 NOTE_D4, NOTE_E4, NOTE_C4, NOTE_A3, NOTE_E4, NOTE_D4
};
//upper staff notes
//notes referenced from "pitches.h" file

int rightDurations[] = {
 2, 4, 2, 4, 2, 8, 8, 1.333333333333333333333,

2, 4, 2, 8, 8, 2, 4, 1.333333333333333333333,

2, 4, 2, 4, 2, 8, 8, 2, 4, 2, 4, 2, 4, 1.333333333333333333333
};
//note durations


void setup() {
 pinMode(red, OUTPUT);
 pinMode(green, OUTPUT);
 pinMode(blue, OUTPUT);
 pinMode(power, INPUT);
 Serial.begin(9600);

for (int thisNote = 0; thisNote < sizeof(right); thisNote++) {
 //for loop goes through each note individually
 int powerControl = digitalRead(power);
 //value contains information on whether the switch is on or off
 int knobValue = analogRead(knob);
 //value contains information on where the potentiometer is (how it was turned)
 int mappedValue;
 if(knobValue < 341){
 mappedValue = 3;
 }
 else if(knobValue < 682){
 mappedValue = 2.2;
 }
 else{
 mappedValue = 1.3;
 }
 //maps potentiometer's values to values that will be used to pause between notes, thus affecting the speed of the music
 //I didn't use the map() function because the values something vary slightly, and that made the music sound odd. It is better to set intervals in this case
 int rightDuration = 1000 / rightDurations[thisNote];
 if(powerControl == HIGH){
 tone(12, right[thisNote], rightDuration);
 int mappedBlue = map(right[thisNote], round(NOTE_A3/10), round(NOTE_D5/10), 0, 255);
 analogWrite(red, 255);
 analogWrite(green, 0);
 analogWrite(blue, mappedBlue);
 //if the switch is on, the note will be produced, as well as the LED color
 }
 else{
 tone(12, 0, rightDuration);
 analogWrite(red, 0);
 analogWrite(green, 0);
 analogWrite(blue, 0);
 //if the switch is off, the note will be muted, and the LED will be off
 }
 int pauseBetweenNotes = rightDuration * mappedValue;
 delay(pauseBetweenNotes);
 noTone(12);
 analogWrite(red, 0);
 analogWrite(green, 0);
 analogWrite(blue, 0);
 }
}

void loop(){
}

Left:

(Same as Right except for the notes, taken from the lower staff in the sheet music)

#include "pitches.h"

const int knob = A0;
const int power = 8;

const int redL = 3;
const int greenL = 5;
const int blueL = 6;

int left[] = {
 NOTE_D3, NOTE_A3, NOTE_D3, NOTE_A3, NOTE_D3, NOTE_A3, NOTE_D3, NOTE_A3,

NOTE_D3, NOTE_A3, NOTE_C3, NOTE_G3, NOTE_D3, NOTE_A3, NOTE_D3, NOTE_A3,

NOTE_G3, NOTE_D4, NOTE_C4, NOTE_D3, NOTE_A3, NOTE_E3, NOTE_G3, NOTE_CS3,
 NOTE_D3, NOTE_A3, NOTE_C3, NOTE_E3, NOTE_C3, NOTE_E3, NOTE_D3, NOTE_F3, 
 NOTE_D3, NOTE_F3
};

int leftDurations[] = {
 4, 2, 4, 2, 4, 2, 4, 2,

4, 2, 4, 2, 4, 2, 4, 2,

4, 4, 4, 4, 2, 4, 4, 4, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2
};

void setup() {
 pinMode(redL, OUTPUT);
 pinMode(greenL, OUTPUT);
 pinMode(blueL, OUTPUT);
 Serial.begin(9600);

for (int thisNote = 0; thisNote < sizeof(left); thisNote++) {
 int powerControl = digitalRead(power);
 int knobValue = analogRead(knob);
 int mappedValue;
 if(knobValue < 341){
 mappedValue = 3;
 }
 else if(knobValue < 682){
 mappedValue = 2.2;
 }
 else{
 mappedValue = 1.3;
 }
 int leftDuration = 1000 / leftDurations[thisNote];
 if(powerControl == HIGH){
 tone(12, left[thisNote], leftDuration);
 int mappedGreen = map(left[thisNote], round(NOTE_C3/10), round(NOTE_D4/10), 0, 100);
 analogWrite(redL, 255);
 analogWrite(greenL, mappedGreen);
 analogWrite(blueL, 0);
 }
 else{
 tone(12, 0, leftDuration);
 analogWrite(redL, 0);
 analogWrite(greenL, 0);
 analogWrite(blueL, 0);
 }
 int pauseBetweenNotes = leftDuration * mappedValue;
 delay(pauseBetweenNotes);
 noTone(12);
 analogWrite(redL, 0);
 analogWrite(greenL, 0);
 analogWrite(blueL, 0);
 }
 }

void loop(){
}
The User

There are three things the user can do with this music box, making it unconventional:

– Play the notes of just the right hand, just the left hand, or both together

– Change the speed of the music (there are three speeds)

– Once they’re playing, mute the notes of the right hand, the left hand, or    both together

Push buttons restart each set of notes, a potentiometer controls the speed, and switches mute the sound (marked with the convention of 0 to “turn off” and 1 to “turn on”).

Circled in red: right push button. Circled in light green: left push button. Circled in light blue: potentiometer. Circled in white: switches

The following video shows how these features work:

See that weird thing that happens at the end of the potentiometer bit? A friend’s hypothesis is that it has to do with the voltage. Regardless, I like it a lot: it makes it seem look like the heart is dying at the end, turnig off in a somewhat organic way. This is kind of the same reason why I didn’t implement switches to fully stop the music once it has started. You can mute it… but the heart keeps on beating until it dies, reaching its natural end.