Friday, December 31, 2010

Arduino, ti amo!

Okay, so I signed on to two more shows, and worked all week. No time for 3d modeling. Did do a little electronics, though. Yes, I do get into a lot of different things!

This particular bit of electronics came out of my experiences composing and running sound for a show on short notice, and my discoveries of the opportunities new technology is making available. In particular, Adaptive Audio; the ability to modify music and sounds on the fly and respond directly to the actors on stage (instead of making the actors slave to the sound design).

I had a test project to try out. A proof of concept. I've done several shows now where an actor "fired" a prop gun, and the sound came from the sound system. And it never sounds right. It's just not possible for a sound technician to press "play" on a CD player (or whatever) exactly at the moment the actor squeezes the trigger.

(And, no, blank-firing guns aren't always the right effect. Having a sampled sound effect means you can tune the emotional effect of the sound; perhaps big and Hollywood, perhaps a period-sounding "crack" instead. With blanks, there's one result and one result only.)

So my thought was; put a radio transmitter in the gun. There's lots of things you could do with the received signal: but it seems most flexible to transform it into a MIDI event. Many pieces of theater gear already speak MIDI; light boards, samplers, some show control software (like for instance Qlab, the program I now run all my sound on.)

The actor pulls the trigger, that closes a switch; and sounds play, light effects start up, pyro is fired, whatever. Once you have a common-format command in a wire, all sorts of things can be commanded by it.

But how to hook it up? It isn't as simple as splicing the right kind of cable. MIDI messages are serial data streams with a very specific format.

Enter the microprocessor.

You can almost think of a microprocessor as a very large hammer. It is overkill for many jobs. But at five bucks a pop it can be cheaper than wiring up a bunch of logic chips (or relays...!) to do the same job.

Take something simple like a button de-bounce. What is that? Well, out in the real world a button has one or more metal leaves that are pushed into contact with each other. As they come together, they spark, jump, bounce back a little, etc. Each of those little jumps is like a little button push of its own. Wire up a counter to that button and it might count one, three, even a dozen times each time a human being put their thumb down and pushed.

So you can do little tricks with flipflops, (the old 7474 chip is nice for this), or use a pair of inverters and a capacitor to store a small delay (the 7404 hex inverter was cute for this). Or you can do something completely analog with an RC circuit (resistor and capacitor).

Or you can do it in code. The pseudo-code might be as simple as;

if digitalIn(button) > 0
   { pressed = 1
   { pressed = 0}

Obviously even five bucks is too much to spend to do that for one button (oh, and an embedded processor still needs clock chip, I/O, power regulator, pull up resistors, et al). But for six buttons? What about six buttons, and you have other tasks for it as well?

Such as logic tasks. Say you have a moon effect that should only happen after the sunset sequence has finished running. You can set this up, pseudo-code fashion;

if (command = TRUE and sunsetEffect = complete)
   { run moonEffect}

But it is in creating a specific output stream that a microprocessor really shines. Want a motor to spin? Find a wire and a 2-way switch. Want it to rotate three times clockwise, once counter-clockwise, turn slowly a half-turn, reverse than gradually come to full speed? Write that into a micro.

MIDI messages certainly fit the bill. Just a refresher course here; MIDI stands for "Musical Instrument Digital Interface" and it was drafted in the early 80's by a consortium of musical instrument manufacturers. It is a cross-industry standard by which keyboards, sound modules, sequencers, and so forth can talk to each other.

Most of MIDI is basically music notation for computers. It is a way of saying things like "Play middle C, really loud, and hold it down for a full quarter note."

A MIDI message is almost always a command byte, followed by one or more status bytes. In the case of noteOn, the command is "play a note on channel 15." The next byte is "play f# above middle C" and the last is "play it pianissimo."

Which in integer form is something like "159 66 23." For historical reasons MIDI is usually written out as hex -- which is a little easier to understand than binary -- but unlike, say, Assembly, there are no standard mnemonics. Well, mostly. NoteOn, for instance, is a standard enough notation most MIDI software libraries will use it as a function.

So this brings us back to the Arduino.

Back when I was a heavy electronics hobbyist the best bet for a CPU was something like a Z80. To make it work, you needed to solder up a whole board; shift registers, I/O, clocks, program ROM, etc. And when you programmed, you programmed in Assembly -- if you were lucky! You might have to burn an EEPROM, or throw micro-switches on a hardware controller, one program line at a time.

Back a few years ago the "BASIC Stamp" made an entrance. Along with Pic chips, these were whole families of basically pre-assembled micro-computer cores. Little PC boards with the microprocessor and support all wired up for you. And packaged with them were the cables, programming adaptors, and software that ran on a host computer -- that is, on your own home computer.

Robotics people really got into them. But there are many, many applications, from hobby to semi-pro, from industry to science, for this sort of cheap, embed-able computing power.

The latest down the pipe is a astoundingly simple-to-use open source project called Arduino. The Arduino boards, based around an Atmega8 or Atmega168, include serial, USB, or Bluetooth ports built in. The software environment runs on Mac, PC, and Linux. And a bootloader is pre-burned in the chip, meaning you can program it with nothing but a piece of cable.

I picked up a pre-assembled "Diecimilla" from Adafruit for a little over thirty bucks. The free environment loaded effortlessly on my Mac G4. The board is about 3" by 2" in size, and is powered from the USB cable. Plug it in to your computer, run the "programming program," stick some code in the window and press the "upload" button. Dead simple.

The programming language, "Wiring," is basically C, but with lots of predefined functions that take most of the sweat and guesswork out of it. I was able to pick up the language in a couple of days -- and I am NOT a programmer.

So I got my first Arduino. First task was to write my "Hello, World!" That actually happened within the first hour. Since I had nothing but a naked Arduino to start with, my "Hello, World!" just blinked the LED pre-wired to pin 13.

Next task was to expand the I/O possibilities. I had also bought from Adafruit a "Prototype Shield" in kit form. Basically, this is an empty PC board with headers (and copies of the status LED and reset button) that sits on top of the Arduino exactly covering it.

Soldered that up, read up on serial I/O with the host computer, and wrote a "Hello, World!" that printed those words on my G4 (into a window on the programming environment) when a button was pressed on the Arduino.

That was the first couple of days. I had bought a bit previously a cute little 4-channel RF remote and PC-board receiver from eBay. The header pins fit perfectly into one of the spare header blocks on the Prototype Shield. Soldered up power leads, ran jumpers to the Arduino's input pins -- then had to figure out how to read the output of the receiver.

Finally figured that out. So now when I pressed a button on the keychain-sized remote, a status LED would light on the Arduino, and in a text window on my G4 I'd get a message like "Channel C Received."

That last was a bit of a hack. But let me remind you; I'm not a programmer. This is the first micro I've ever played with. And I was last really busy soldering up blinking LEDs somewhere back in the 80's. The stuff now is almost disturbingly easy. One more fall-out of the digital revolution.

Close of the week (okay, Sunday) and the last step. Wired a MIDI jack to my prototype shield, as per some schematics found online. Stuck that in the "MIDI in" port of my old Yamaha QY10 (a portable sound module and sequencer). Then spent quite a few delightful hours making stupid mistakes about MIDI. I thought I was already fluent, due to a long history of composing SystemExclusive messages for sound modules (don't ask!) but I still made some really basic mistakes.

Until, finally, my test program made a note play.

At which point it was a really quick hack to open up the RF Receiver program I had written a few days earlier, plug in the correct Serial.print commands, and....

I could now play piano notes from across the room.

Which by itself doesn't sound like much. But such is the nature of a proof-of-concept. The proof-of-concept here is that a thirty-dollar embedded microprocessor can be easily programmed to create a custom MIDI message or sequence of messages upon a specific hardware trigger.

So I could, right now, hot-glue the keychain remote into my plastic replica Luger, put a gunshot sample in my Boss "Dr Sample," and have a realistic-sounding stage gun that plays out the big theater speakers.

Or stick a larger button on the Arduino, tape it to the wall of the set, and set up a MIDI-fired hot-key cue in the software that's running sound on the current show. Enter a better doorbell sound; actor-triggered, played through the sound system, with tonality and volume completely under control of the designer.

But why stop there? See, the inputs aren't limited to on or off. I can interface with capacitive sensors, motion sensors, light sensors. I can set up parameters under which the chip will react to the sensors, and I can abstract a continuous flow of data. Say, a position sensor connected to a window. When the actor opens the window, sounds of street noise fade up with it. Or, say an ultrasonic sensor; when an actor gets close to the magical tree, tiny lights begin to twinkle and shimmer in interesting patterns.

Because output is of course not limited to MIDI messages. I can throw relays, light displays, run motors, even communicate to Max or Flash on a connected computer. People have attached these things to cameras and cell phones. Made self-contained musical instruments out of them, and wearable light shows.

I look forward to seeing what theater-related ideas I can come up with for mine.

This is a old entry from my previous journal. Since writing this I have made extensive use of the core technology whose development I described here, and am on version #3 of the hardware. Among the things I have done is check a monitor system from the stage by triggering keyboard or sound samples from my keychain remote, step through and listen to all the sound cues in a show from out in the house, fire off sound effects from a remote button located in the orchestra pit or just over by the Stage Manager, and even created -- but have not yet used in production -- a MIDI-controlled video projector douser.

No comments:

Post a Comment