1. 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.
2. 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).