Communicate data between Pure Data and an Arduino using the USB serial bus.
The Arduino IDE and Pure Data solve different problems well. The Arduino has easy direct access to its hardware interface, and can create and respond to electrical signals with very low latency. Pure Data is well-suited to interactive real-time programming, but the latency and jitter is considerable higher than on a microcontroller, and on a laptop lacks access to hardware interfaces. This exercise will demonstrate one way of coordinating Pure Data and the Arduino to take advantage of the best features of each by linking them using a simple ASCII message protocol over the USB serial bus.
The serial communication does introduce significant latency between the Arduino and Pd, so this approach is strongest for systems in which Pd is used for tasks such as the GUI, visual display, sequencing, and overall event control. It would not be suitable for high-bandwidth tasks such as cycling individual digital outputs to create a stepper motor waveform. However, the extensibility of the protocol means that you can add those low-level hardware behaviors to the Arduino client program and invoke them via messages.
Setting up the exercise requires a couple of preparatory steps:
The OneInOneOutASCII sketch is provided in the course distribution in the folder named support/Arduino/OneInOneOutASCII. If you have not cloned the course materials from github, you can download it directly from github. The source code of the sketch provides details of the messaging scheme
The Pd patch is available in the same materials folder as this file or directly from github.
This particular Arduino IDE sketch is going to serve as the basis for all the following exercises in which we connect hardware to the Arduino and control it from Pd.
For a challenge, examine the source code for both the sketch and the patch and work out how to add additional message types.