Exercise: Melody Player in Tinkercad

Sound is a physical vibration. We can produce sound electronically by using an actuator such as a speaker to transduce an oscillating current into air vibrations. The Arduino is not especially capable with regard to producing audio waveforms, but it can easily produce tones, so we’ll use this as a springboard for exploring computational structures.

An Arduino is capable of programmatically manipulating the outputs on and off fast enough to create audio pitches, colloquially called bit-banging. However, the Tinkercad simulator isn’t as fast as a physical Arduino and so while it can be fast enough to produce some tones, this approach will quickly reveal the limits of simulation. Instead, we’ll focus on using the tone() function which simulates the Arduino hardware-based square wave generator.

The tone function accepts integer frequency values. If you’d like to produce musical tones you’ll need a table or function to map more familiar pitch names to frequency. A Twelve-tone equal temperament in Western music uses an exponentially-spaced pitch system in which the pitch of each semitone is \(2^{1/12}\) times the previous semitone, and concert A is exactly 440 Hz. For a sample function implementation of a pitch function, please see my arpeggio example in the Lecture Sample Code. For an approach using symbolic constants, please see the standard toneMelody Arduino example.

Objectives

After this exercise, you should be able to:

  1. Apply elementary musical acoustics theory to generate audible tones.

  2. Use digital inputs to control program flow.

  3. Use analog inputs to parametrically control a computational process.

  4. Write code to generate scripted action sequences.

  5. Write code using parameterized functions to implement action primitives.

  6. Write code with integrated debugging using the serial port for user console communication.

Reference Guides

Please review the following reference guides as needed:

Steps and observations

The overall objective is to create an automatic sound synthesis device with parametric user controls. The following checklist will guide you through building a melody player circuit and program to submit within a single Tinkercad sketch.

  1. Please construct a circuit with the required minimum components as listed in the Deliverables section: an Arduino, piezo speaker on a digital output, two switches controlling digital inputs, and a potentiometer wired as a variable voltage source controlling an analog input. For clarity, please use a breadboard to organize the discrete components.

  2. Set up the shell of a text-based Arduino program to support your hardware, e.g. initialize the Serial port, initialize the output pins.

  3. Add temporary code to test your inputs and outputs: can the Arduino read the switches and potentiometer and print the values correctly? Can it generate audible pitches with tone()?

  4. Choose one or more musical objectives before you start coding the main synthesizer. Do you want to play a familiar melody? Would you prefer to algorithmically generate a tone sequence? Will it use conventional musical tones, an idiosyncratic set of pitches, or smoothly varying frequencies? Can the composition be broken into phrases or repeating sections which could be written as functions?

  5. Choose one or more variational objectives to be driven by the user digital and analog inputs. E.g. Could the user smoothly adjust tempo or tuning? Could the user select alternatives in melody structure or chromatic key?

  6. I recommend structuring your program around several parameterized functions. The best choices of structure depend upon your objectives, but here are some suggestions:

    1. A structured musical passage could use functions to generate phrases and higher-level functions to string together phrases.

    2. The input processing might be best managed with a polling function which can be invoked from multiple points of execution.

    3. A data-driven system might use tables to specify pitch or phrase sequences, and one or more player functions to generate output from the data.

    4. A semi-autonomous approach might use an

  7. Implement your ideas in code, then test and iterate. Please add debugging output to the Serial Monitor as needed.

  8. Please be sure to include user instructions in comments at the top of your code.

Deliverables

The result of your explorations should include:

  1. a single circuit sketch including code, submitted as a URL to Canvas

  2. a brief paragraph describing your melodic intent and outcome, submitted as text to Canvas

The circuit sketch should contain the following components:

  1. Arduino Uno

  2. Piezo speaker for audible output

  3. At least two switches for digital user input

  4. At least one potentiometer for analog user input

The Arduino should contain a text-based program including the following behaviors:

  1. Brief comments with user instructions.

  2. At least one user-triggered algorithmic tone sequence.

  3. At least one user-selected discrete variation in the audio output.

  4. At least one parametrically-controlled continuous variation in the audio output.

  5. Some useful information printed to the Serial Monitor during operation.

When it is time to submit your result for the assignment, you’ll need to click Share, then Invite people. You may then copy the generated sharing URL and submit it to Canvas.

N.B. As of Fall 2020, Tinkercad has a sharing bug, please note the following. Tinkercad Circuits sketch sharing links which include the term ‘tenant=circuits’ appear consistently to return a 404 error instead of accessing the sketch. The first workaround appears to be to exit and reload the sketch at least once before generating the share link. Alternatively, an existing link can be fixed by editing out the tenant=circuits clause, e.g. delete the text ‘?tenant=circuits’.

Challenges

If you would like to explore more, please consider the following optional challenge questions.

  1. Try developing an algorithm to produce a non-repeating musical sequence. A trivial solution is just to use the built-in random generator, but can you produce an infinite sequence which conveys a sense of intentionality?