In this post I want to sketch out how one can control actuators (like solenoids, motors, leds) connected to an Arduino in a rhythmical/musical way.
I want to propose two ways one could do that:
Internally: programming structures in the Arduino language.
Externally: Using external equipment or software.
This post was written for students of a 1 semester long Arduino course at the University of Applied Arts Vienna at the class for Transmedia Arts.
The idea for the course was inspired by Moritz Geist’s „Sonic Robots“ project.
Arduino runs code on board and triggers connected actuators.
It’s very nice because:
- Project can run standalone without the need for an external computer.
You could even add a battery to make your project truly mobile.
- Sketching, developing and finalizing your composition purely in code is quite different than working with a (graphical) user interface as modern sequencers provide. It will challenge you to think more systematically about the (rhythmic) structures of your composition.
(different kind of aesthetic.)
What I don’t like
- As you are sketching out your composition in code, it can be quite difficult to keep an overview of the composition. (No overview of sequence.)
- Timing of events is easy if you just have one actuator connected. In the case that you want to time more than one event, you will run into the “delay() problem” (see „Beware of“ section).
- Write functions that encapsulate patterns. (Microaesthetic)*
- Parameterize functions to easily test variations, add potentiometers to enable direct control of parameters. (This way you can skip uploading new code when changing parameter’s values.)
- Combine these patterns to realize generative compositions. (Macroaesthetic)*
Make yourself comfortable with how functions work in Arduino.
- This example triggers two actions: one with the probability of 80%, the other with probability of 20%.
- This example triggers two separate triggers. But we will run into timing problems because of the way delay() works.
Beware of the delay()-problem!
Using delay() in your code actually halts your Arduino for the specified time. This is fine if you just have one event that you trigger using delay. (For example moving a motor every second, thus using delay(1000) in the loop-function of your program.)
When triggering more than one event however you will run into problems, as delay times add up. Have a look at this sketch to experience the delay problem in full effect.
To conveniently work with multiple events I strongly suggest using one of many many many timer libraries available for Arduino.
I worked out a few examples using the ‚Arduino Timer Library‘ by Doctor Monk (No, it doesn’t support PWM.):
External soft- or hardware send MIDI signals to Arduino which in turn triggers connected actuators.
Everything that transmits MIDI works.
(MIDI (Musical Instrument Digital Interface) enables musical instruments and computers to exchange control information.) https://en.wikipedia.org/wiki/MIDI
There are multiple ways to send and receive MIDI data with Arduino*.
In this tutorial I want to focus on ways that don’t involve flashing the firmware or external circuits.
It’s very nice because:
- MIDI software and hardware is built to provide intuitive ways to make music and/or compose pieces.
- Using hardware MIDI controllers make it easy to play your project life (eg in a performance or band situation.)
- Software sequencers can come in especially handy if you work on long compositions
- By using MIDI technology it becomes very easy to sync your project to other electronic instruments and/or sounds.
- There’s no issue with triggering multiple actuators. (No delay() problem.)
- No need to reupload code to Arduino as you change your code in the Processing sketch.
What I don’t like:
- Projects are dependent on a PC or Mac.
- Setup can be confusing.
This is quite a mashup.
We are going to use the possibility of Processing to both handle MIDI as well as control Arduino boards. (Processing is an open-source programming language especially aimed at beginners.) The interesting thing about this approach is that you end up programming your Arduino’s behavior in Processing. This might seem a bit weird in the beginning, but actually isn’t to much different from programming the Arduino directly.
MIDI device -> MIDI -> Processing -> Firmata -> Arduino -> solenoid/motor/etc.
Here is how to proceed:
What you need:
PureData, Sonic Pi for a more generative approach
Music sequencing software like Ableton Live, Cubase, etc.
Hardware: MIDI keyboards, hardware sequencers like Arturia BeatStep, Elektron devices, etc..
Processing to run the Firmata sketch.
MidiBus and Firmata libraries.
If you just wanna use external MIDI controllers, there is no need fo virtual MIDI ports!
I prepared an example Processing sketch you can use as a starting point.
• Upload StandardFirmata.ino to your Arduino (You can find it in Arduino IDE’s menu: File->Examples->Firmata->StandardFirmata)
• That’s it!
• Connect external MIDI controllers/keyboards to your computer and/or
setup virtual MIDI on your PC/Mac.
• Download Processing from processing.org/download.
• Install „Arduino(Firmata)“ and „The MidiBus“ library through the Library Manager
(Select “Add Library…” from the “Import Library…” submenu within the Sketch menu.)
• Open example file MIDI_Firmata.pde
• Run example file and watch the console for Available MIDI Devices.
• Change the first number in line „myBus = new MidiBus(this, 0, 0); „ to select the MIDI port you want to receive MIDI from.
This could be an external MIDI keyboard, if you want to use hardware directly, or a virtual MIDI port like IAC.
• Look for available serial ports in console and change the index number in „arduino = new Arduino(this, Arduino.list(), 57600);“ (In the brackets after ‚Arduino.list()’) to the port your Arduino is connected to.
• Stop and restart the sketch.
• Test your MIDI setup: Play a few notes or move rotary controllers and check Processing’s console if it successfully received those MIDI events.
• The build in LED on pin 13 on your Arduino should also light up when playing MIDI notes.
• Connect actuators and change the values of triggerPin1, etc. according to the pins you connected actuators to.
• Rewrite noteOn() and noteOff() to your needs.
Arduino Zero, DUE or 101 boards can be set up to work as a standard MIDI device https://www.arduino.cc/en/Tutorial/MidiDevice
Teensy boards can be set up to work as a standard MIDI device. https://www.pjrc.com/teensy/td_midi.html
Also certain other Arduino boards can be turned into a native MIDI device by flashing the firmware.
You could build a MIDI input connector circuit and connect it to Arduino.
Or get a MIDI-Shield. https://www.sparkfun.com/products/12898
(A good way to connect powerful actuators to your Arduino is shown in this tutorial: https://www.bc-robotics.com/tutorials/controlling-a-solenoid-valve-with-arduino/)