Arduino Reich Relay

As a follow up to my Arduino Relay Bolero I started to think about other things I could do with the relay and remembered Steve Reich‘s Music for Pieces of Wood, written for five percussionists to play on five pairs of tuned claves.  I had five relays, so sought out the score and got to work.

Now this rhythmic work is very mathematically constructed with patterns being introduced over the top of the original patterns.  There is an excellent visualisation of the work here and many excellent performances on YouTube.  So it really lends itself to being programmed into a microcontroller.

However, to keep things efficient in terms of coding, I’m not going to use the standard Arduino I/O functions.  So if you are new to Arduino, I recommend having a play with the Arduino Relay Bolero project instead.

These are the key Arduino tutorials that are relevant to this code.

Parts list

  • Arduino Uno
  • 5x Relay modules
  • Jumper wires

The Circuit

Arduino-Relay-x5_bb

The circuit is the same as that from the Arduino Relay Bolero except that there are now five relay modules all connected to digital I/O pins 2 to 6.

The Code

This takes a similar idea to the Arduino Relay Bolero but instead of using the delay function for the durations of the notes, this program maintains a steady “tick” and there is a set of instructions which tells the code which relays to “play” on which tick.  A bit like the holes in a pianola roll.

Once again, I use the open and closed clicks of the relay interchangeably, so when it comes to “play” the relay, I see what its current state is and toggle it to the other state. This means that each part in the piece actually as a slightly different sound for the open and the close, but I quite like that.

In the interests of efficiency, this code is using direct port I/O, not the Arduino digital I/O functions.  For the Arduino Uno, digital pins 0 to 7 are representations of the 8 bits for PORTD of the microcontroller – each bit in the port corresponds to the same bit in the Arduino Uno’s view of the world.

The “score” is encoded into a huge array of numbers representing the pattern of relays to play on each “tick” of the music.  This was too much to store in the Arduino’s data memory all at once, but as it doesn’t change, I used a trick to put it in the Arduino’s program memory instead (there is more of this type of memory on the board).

This means defining the list with the PROGMEM keyword and when it comes to retrieving values from it, using the pgm_read_byte_near() function.

I’ve also used binary values when setting up the numbers for the score, as each digit then represents one of the relays to “play”.  These are specified using the format 0b00000000, although as I’m only using five relays, I only use five binary digits for each one.

There is a range of bit manipulation going on in this code.  The basics are as follows, but see the tutorial on bit manipulation for full details.

value |= bitvalue; // this sets a bit
value &= ~bitvalue; // this clears a bit
value &= MASK; // this "pulls out" certain bits according to the value of MASK
value &= ~MASK; // this "pulls out" the other bits (not specified by MASK)
value = oldval | newval; // this "adds" the newval to the existing value in the port
if (value & 0x04) {} // checks the third bit (0b00000100 = 0x04) and if set does something

Note that any references in the code to PORTD, PIND, or DDRD are directly accessing the Arduino microprocessor’s I/O port D directly and thus actually controlling something.  Also, as I’m only using five of the pins, and pins 0 and 1 are used for the serial port, I try to be careful not to mess with the values of the pins I’m not using.

Find the full example code on GitHub here.

The full score definition of the Steve Reich piece of music was included in a separate file (as it is quite large).  If you are interested, give me a shout.

Closing Thoughts

The relays all sounded very similar, which was an interesting effect in its own right, but what really made it work for me was mounting each relay, using some blutack, onto a different resonant surface.  This gave each one an individual sound.

I also think I could probably build this piece of music up programmatically and save on memory, but in this version, I’ve just hard-coded in each beat of music, including the repeats.

In the future it might be interesting to get the relays to actually drive something else electronically.

2020-06-03 20.37.45

Kevin

 

Leave a comment