teaching machines

Chorder, Part II

July 13, 2018 by . Filed under electronics, music, public.

This post is part of a series of notes and exercises for a summer camp on making musical instruments with Arduino and Pure Data.

In the last exercise, we proved to ourselves that our hardware was working. In this next exercise, we want to extend our Arduino program so that when a tomato-capacitor is touched, we send the MIDI numbers of the notes in that tomato’s chord out along the serial port to Pure Data. For instance, if we touch the tomato and it represents C major in the 5th octave, then we issue 60, 62, and 64.

You’ve coded a fair bit by now, so less code will be given to you in these lessons. We’ll describe what needs to be done at a high level, and you and your partner can reason out what the code should look like. You are always free to ask for help. We just think your brain will get more wrinkles if you come up with the code instead of us.

Only On Change

As with most of our instruments, we only want to act on the sensor inputs when there’s a change. Declare a global variable to hold the old bits, and then get the new bits in loop.

At the very end of loop, assign the new bits to the old bits to prepare for the next iteration.

Processing Bits

In loop, we want to walk through each of the several capacitors and see if any of their bits flipped from on to off or from off to on. (That’s a lot of prepositions!) Write a loop of this form:

for each bit i
  old_bit = get old bit i
  new_bit = get new bit i

  if the bits are different
    print i 

But use real C++ syntax and the functions that we’ve talked about.

Upload your code and test that you see individual bit indexes when you touch an alligator clip.

Incorporate Scale

Declare two global variables:

These two variables will combine to produce the MIDI numbers for the notes in the scale.

Offset 0 corresponds to capacitor 0, offset 1 to capacitor 1, and so on. When capacitor i is touched, we want to emit that capacitor’s chord. The first note of that chord is the note whose number is the root plus offset i. In loop, when the bits are different, print the root plus offset i to the serial port.

Test your code before moving on.

To produce a full triadic chord, we also want to print the note two positions farther on from the first note.

How do we calculate the next note’s MIDI number?
root + offsets[2]

And we also want to print the note two positions farther still.

How do we calculate the next note’s MIDI number?
root + offsets[4]

Upon testing your code, you should see three note numbers every time you touch or release an alligator clip.

Printing State

Our last order of business is to print the state of the capacitor. If the new bit is 1, we print a 1. If the new bit is 0, we print a 0. In the Pure Data patch, we’ll use this state to figure out whether the notes should start or stop playing.

Upon testing your code, you should see four numbers every time you touch or release an alligator clip. The first three are MIDI numbers and the last is a 0 or 1.

Challenges

  1. How did you code the printing of 0 or 1 in the last step?
  2. What mistakes did you make while writing this code?