The Arduino function "note" in Figure 1 calculates the period for a given frequency, and then plays that note. I have borrowed this code from Arduino Forum user "raschemmel" as published on this website: https://forum.arduino.cc/index.php?topic=343270.0.
I setup a big array containing notes in the first row, and durations in the second row. The notes and durations were defined in musical terms making transposition from sheet music relatively painless although tedious (Figure 2). The full array defining the song can be found in Github with the rest of the Arduino sketch. Finally, playing the song itself is as simple as a double "for" loop which iterates over the columns of the song array and plays the note for the duration specified in the second "for" loop. I'm proud of the simplicity of the loop function (Figure 3). I also included two scalar variables (tempo and pitch_multiplier) which modify all the duration variables (tempo) or pitch variables (pitch_multiplier) allowing for transposition and tempo control.
While this setup worked, the volume of the stepper was too quiet even when I mounted it on a large piece of metal to use as a resonator. I played around with a piezoelectric pickup and an amplifier circuit (see descriptions later in this post), but the piezo picked up too much vibration noise from the motor and sounded pretty awful.
I stumbled on Moppy, which seems to be the most popular way of playing music from an actual floppy drive. The principle is presumably similar to my Arduino sketch, except that you feed it a midi file (the equivalent of my array) and it provides a clean Java based user interface. In addition, Moppy supports multitrack midi files so that you can play different "voices" with different floppy drives.
Setup of the floppy was pretty easy once I figured out which pins to connect to the Arduino (Figures 4 and 5).
I modified an existing Star Spangled Banner midi file to leave only the melody line using the program Anvil Studio. The result is shown in the Youtube video below.
Finally, I used an LM386 chip to breadboard an amplifier circuit driving a small 8 Ohm speaker with the piezoelectric pickup mounted on the case of the floppy drive (Figures 7 and 8). The circuit was adapted from CircuitBasics. This worked much better than mounting the piezo directly on the stepper, and compounded with the fact that the floppy was much louder than the stepper, I was able to record a decent rendition of the song. I will be playing the video at the opening ceremony of a robotics tournament which my school is hosting.