Chorder, Part II
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 that 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 in their state. As we’ve done previously, 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:
read new bits for each bit i old_bit = get old bit i new_bit = get new bit i if the bits are different print i preserve old bits
But use real C++ syntax and the functions that you learned about in the previous exercise. Also, you can remove any calls you have to printBits
. We won’t need that function any more.
Upload your code and test that you see individual bit indexes when you touch an alligator clip.
Incorporate Scale
Declare two global variables:
- The MIDI number of the root note that you want for your instrument. For instance, if you want your scale to start at C4, you’d assign 60.
- An array of offsets from that root that produce a major scale. The first offset is 0, as the scale starts at the root. The second note in the major scale starting at 60 would be 62, which is an offset of 2. The third note is 64, which is an offset of 4. Use the list of offsets you wrote down earlier.
We’ll use these two variables to determine 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[i + 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[i + 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.