(Update : a reader (dbfreq, see his messages in the comments) found a bug in the patch. There was a typo in the route object in the arduino abstraction. The file available for download has been fixed, but I haven’t changed the picture in the post.)
I first made an abstraction called [pd arduino], in which i put all the stuff related to the [arduino] object. As you can see in the picture below, we can send 2 different types of messages to this object : “open 5” will connect to the arduino board (mine is on port 5, yours can well be on a different port), “close” will close the port (if you want to remove your arduino safely!), and “pinMode 19 analog” will open pin number 19 as analog input. The “pinMode” message can be adapted to open any pin for input or output on the arduino. This is where the power of Firmata lies : it implements two-way communication between Pd and the board in a very easy way. Please refer to the “arduino-test.pd” patch in the Pduino folder for a more complete explanation (it’s a good thing to play with this test patch with different things connected to the board – buttons, pots…).
Once the board AND the pin are open, data starts coming out of [arduino]’s output. We use a [route analog] object to only get analog data (we’de use [route digital] to only get digital data). Then we use [route 5] to only get the value of a pot connected to analog pin 5 on the board (the fact that we had to open pin 19 to read analog pin 5 is not very intuitive, but it’s actually pin 19 among all pins, and pin 5 among analog pins only). The remaining objects in this abstraction are only there to normalise the values from the pot between 0 and 1 (if you can turn your pot over it whole range you’ll get values between 0 and 1000 and it’ll be easier to normalize).
Ok, so now we have the value read from the pot coming out of the [pd arduino] abstraction by its outlet. Great, that was the hardest part. I’ve made a small patch around this abstraction to use the values from the pot to control the length of a simple delay line with feedback. I multiplied the output of the abstraction by 500 to have a range between 0 and 500 (to get audible changes in the delay length), and then used a combination of [pack 0 100] and [line~] to control a [vd~] object. The later reads from the delay line a length controled by an audio signal (to have smoother changes), so we needed the [line~] to change the raw values from the pot into an audio signal. I put 100 in the [pack] to have a slow ramp in [line~] every time the value changes to make things even smoother. The rest of the patch should be pretty straightforward (if it not please refer to this post).
Please note that in order to use this patch you’ll need :
- An Arduino board…
- To upload the Firmata firmware on it (it is available in the standard Arduino IDE),
- A potentiometer connected to the board (see tutorial here),
- The Pduino “package”, which you can download here,
- To know on which port your board shows up. You can send a “devices” message to the [arduino] object to see a list of ports to which devices are connected. Your board should be on a port higher than 1. Or, you could very easily adapt the abstraction I made for a previous post to scan all the ports and automatically detect your board.
Here’s a sample of the sounds you can make with this very simple patch :
And here’s the patch.