SERIAL COMMUNICATION
This tutorial covers the topic of Serial Communication between the Arduino and Max/MSP. The “serial” in Serial Communication means that the data is transferred one byte (8 bits) at a time, and is immediately followed by another byte until the data transfer is completed. Arduino and Max/MSP communicate via a serial line over USB. The tutorial Max/MSP patch and accompanying Arduino code has been implemented to showcase the data transfer possibilities between Max/MSP and Arduino. The Max/MSP patch contains several sub-patches: Serial Config, Analog In, Digital In, Digital Out and Analog Out. In example below, the Arduino does not read any sensor data – it waits for a message from the host computer and once received it begins a serial response.
int serialMessage = -1;
// Message Received Flag
int messageReceived = 0;
// Send Random Value to Host
long rnd = 0;
void setup()
{
// Set the serial baudrate, used internally by arduino
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
serialMessage = Serial.read();
Serial.flush(); // Clear Serial Incoming Buffer
messageReceived = 1;
}
if (messageReceived)
{
rnd = random(1024);
Serial.print(rnd ); // Print rnd value
Serial.print(" "); // Print Space Char
Serial.println(); // Print LineFeed
delay(50); // Pause
}
}
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.


Cycling 74: Makers of Max/MSP & Jitter. Arduino: Technical Data regarding Arduino. Ladyada: Arduino Information & HowTo’s. The Max/MSP patch was created using Max/MSP v5.04. The Sensor Lab has Max/MSP v5.04, Arduino to Max Tutorial Files.