Serial Communication

1. Max patch and Arduino Code examples

Sensor – Arduino – Max

Analog Input (0 – 1024)

: Arduino reads analog signals through its ADC – analog to digital converter. These converters read a voltage value and transform it into a value encoded digitally. So, 0 volt is represented by number 0, and 5 volt is represented by number 1024. 

Photocell = Flex Sensor, Analog Infrared proximity sensor, Force sensor

Digital Input  (HIGH / LOW)
Button / Internal pull-up resistor
3 Button, Max patch / Int
PIR sensor

Max – Arduino – LED/Servo/Stepper motor

Digital Input
LED On/Off

PWM : getting analog results with digital means
LED Dimmer
RGB LED Dimmer

Shift Register
 – Arduino code and Max patch
74HC595 Example – Arduino code and Max patch

TLC5940 + 74HC595(int)

2. Send int data to Max/MSP

As using Serial.println(); method, we can send a data as an intiger format. In this case, the sensor value can be expressed between 0 -1024. However, since Max/MSP receives the data as ASCII format, it requires some conversion of data.

If you are using Serail.println(); method in Arduino, you will need to have the patch like below.

Where we need to look carefully is from select 10 13.  10 means new line and 13 means return. As using println(); method, we can detect the new line and return to distinguish new data.

Once it recognizes that it received a new data, it will group the information. It means, after a new line (10) till return(13), Max/ MSP will receive a several ASCII code information, like (10) 49 48 50 52 (13) for 1024. So, zl group will help you to group the separated character information as one data. Then, itoa is an object to convert intiger to Ascii code. fromsymbol object convert ASCII into number, which can be suitable in the number box. So, if you want to use Serial.println(); method in Arduino to send number range from 0 – 1024, you need to distinguish data using “select 10 13” object : read from new line till get a return. Then you need to have “zl group” object to combine individual characters to make one completed data like 1024, (sum of 1 , 0 , 2, and 4). To convert data for number box in Jitter, “itoa” (intiger to ASCII), and “fromsymbol” objects are needed.

3. Serial Communication with Serial.wirte(); in Arduino

If you do not want to have a bunch of objects after serial object, there is a way you can send byte data directly. Serial.write(); object will help you to simplify your patch. Serial.write(); writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead. Unlike print(); method, Serial.write(); sends data in byte format, which has the smallest range out of data types(-127 to 127, 0 – 255). As communicating with binary data, we can get rid of many objects for data conversion. So, when you have Serial.write(); method in Arduino, you can connect number box directly to serial object outlet.

TIP: There is anther thing we need to take care of  in this case. If you look at the data range carefully, you should notice that we need to map / scale the incoming data from sensor. Analog input sends data ranged from 0 – 1024. However, byte data can express only from 0 – 255, a quater of 1024. So, values after 255 will be wrapped (sensor value  256 will return to the minimum number 0, and start again to count till 255). That is why we need to divide anlalogRead value by 4, in order to make the maximum value as 255. We can use map(val, fromLow, fromHigh, toLow, toHigh) to scale/map the value from sensor (Analog Input).


patch download

4. Multiple input from Arduino to Max/MSP/Jitter

Serial communication is the process of sending data one bit at a time, sequentially, over a communication channel or computer bus, which means you can send data from one to the other one at a time. So, to send multiple data through serial communication, we need to combine multiple data as one data, then split it into multiple data later. Jitter has a very handy object to split a list of data into individual data, named “unpack”. In Arduino, we can make a list of data as using exceptional character “Add” operator in programming.

In Arduino, firstly store values in different variables. Then, we can print/send data in series, then separate them putting ” “(space) in between. Look carefully that we are using Serial.print(); – print without new line, rather than Serial.pritnln(); – print with new line. We are using Seiral.println();  only one time at the end. That means, it is sending a set of data without new lines to make a list, then whenever it starts to make a new list, it will start with a new line. The actual data which would be sent is : “val1 val2 val3”

If you remember that Max/MSP separates individual data as reading from a new line until return (ASCII code 10 and 13) in our general patch for serial communication (receiving data as intiger, not byte), you can notice that val1, val2 and val3 would be read as one data until it hits Serial.println(); after “val3”.

The received data “val1 val2 val3” in Max/MSP will be separated as going through “unpack” object as “val1”, “val2” and “val3″. Also, we need to send the data as intiger (symbol/character) because we send the data as a list including ” “(space). It cannot be sent as byte.


Download Patch & Code

5. Sending Multiple Input data from Max/MSP to Arduino – using Shift Register

We can think of two possible ways to send data from Max/MSP to Arduino. 1. Sending data as a list with index/address and value together, 2. Sending data one at a time, index/address first followed by value. In the case #1, it seems more clear and straightforward way to transferring data. However, the space ” ” between index character ( like ‘a’,’A’, ‘b’…) and value (mostly int) cause redundant buffer while transferring data to Arduino.” “(space) is also recognized as a kind of data, so Arduino reads index number and ” ” (space) as a data. So, your actual value after space is stored and sit there waiting for next signal (bang or trigger).

So, instead of sending data together, we can send necessary data separately  to get rid of redundant data, like space. Also, we are going to convert symbol value to intiger. What we need to be careful at this point is that if you send a number in more than two figures, it would be read as a combination of characters, like 10 -> 1 + 0(ascii value 48 49). Then it would create a space between two numbers. So, we need to stick to one digit number between 0 to 9 to avoid confusion for Arduino in Serial Communication.

Once transferred data is read correct at right place, it could be mapped (for PWM value) or used for boolean test (to set HIGH or LOW value).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s