Arduino and Max

Controlling Max with an Arduino

In a previous post we examined the process of getting the data from a Micro:Bit into the Max software via Python. I like using the MicroBit because it already has onboard sensors such as compass, accelerometer, etc.

However, at some point we want to add more powerful code or more sensors to this system and this is where MicroBit can be limited. Arduino (aka Genuino) is one of the most popular Micro-controller Platforms that exists currently with well over fifty iterations of the board commercially available (cite).

Unlike the MicroBit it is (easily) coded in C/C++ and, as such, we can quickly implement existing C code to control output devices, read sensors, etc. providing a good platform for developing audio tools.

This article looks at getting the sensor code from Arduino into the Max software and uses the exact same patch to read data from the Serial Port that we did in the MicroBit post.

The code to the right is displayed in the Arduino IDE and is a version of C. This basic code is reading data coming into pins A0 and A1 from a joystick controller. It outputs the two values to the serial port separated by a space. Each value is 0 to 1023, which is a 10 bit value 0 to 1111111111.

As stated in the previous post, getting any serial data into Max is a straight-forward process, here the Serial Object is used, the baud rate is set (anyone my age will remember baud rates from our dial-up modems) and the port which the Arduino is plugged into defined.

My Mac only has two USB ports so it's going to be either a or b. The baud rate is set to 9600 in the example but, again, experiment to find what suits your system/s best.

Once that's the serial data can be decoded and unpacked to separate outputs which can then be routed to control anything in Max!

The image to the left is the implementation in Max. It has less outputs than the MicroBit patch as we are only sending two pieces of data currently. I have used a pictslider object to visualise the input of the joystick.

The data popups are for debugging purposes.

Currently the unpack object is taking the two values and separating them back out again in order to implement the x and y values of the joystick (or any other sensors you care to use).

As you can see from both this post and the MicroBit post, the patch to decipher the incoming serial data is the same in both cases. Therefore you can use it to bring any serial data into Max from anything that outputs serial data. You can also use a serial to USB converter to bring the serial data in via USB. This is build into most Arduino boards but is also available as a self-contained circuit board.

A future post will examine the options for a wireless implementation of this process.