[sharing is caring] Arduino + max

This semester I’ve had a number of students ask me about getting sensor data from the Arduino into Max. Seeing as how a component of the class involves actually writing code for the Ardy, I don’t even mention Firmata, Simple Message System, etc.

Luke’s example in the Max tutorials is good for showing that the Arduino and Max can communicate, but has confused some of the people in my class, particularly those who are not well accustomed to patching.

This simple example illustrates multiple analog inputs on the Arduino going into max as bytes. There’s a simple handshake protocol involved so the Ardy only reads the Analog Pins 0 & 1 when Max requests the information.

The Arduino code isn’t commented, but is very straightforward.

Suggestions for improvement are quite welcome.

~s

max5 serial comm with arduino

-- Pasted Max Patch, click to expand. --

Copy all of the following text. Then, in Max, select New From Clipboard.

hi, thanks for sharing this.. as a new arduino user myself, im glad to see more stuff to learn from.

i get all the program except the last two lines.. could you comment on the Serial.print(Val1/4,BYTE); line?

i understand you’re sending the values of the ADCs serially as a byte, but why val1/4? is this because arduino sensors encode the ADC signal to 10 byte values (1024), and 256 (1024/4) is 8 bit (as is a byte)?

also, to me it looks like you have an unused variable, int inByte… can you comment what it does?
thanks :)

As for the inByte, it’s local and just thrown out after each loop. In the max patch, after a valid packet is sent (Val1, Val2, 10, 13), max sends a byte to the Ardy, letting it know it is ready for more data. The inByte just reads this out of the serial buffer before sending the values from the pots.

second, i try to put some text from Max5 to a 16×2 LCD display, via an arduino board.
All is right with hardware but i’m not fluent in C and C++. So i need help to find a way to tell arduino to understand ASCII data string like "hello world!".
I suppose i need to declare this string, read it from serial, and then load it to LCD via lcd.print…

As an academic and practical matter, I find it’s best to be as explicit with things like that as possible. Plus, one % is not going to hog up massive amounts of resources.

As for the ASCII to Ardy, I think the nuts and bolts of it is best left to the Arduino forums. On the Max side, you would simply need to send the message "Hello World!" out the serial port (very bare bones example below):

— Pasted Max Patch, click to expand. —

Copy all of the following text. Then, in Max, select New From Clipboard.

IMO, it is inevitable when working with Arduino or any microcontroller that you’ll have to eventually do some bitshifting and bitmasking. You will be much happier in the long run to spend a little time getting used to it. This will give you the most efficient and lightweight compiled machine code. For this example, I would do the following:

Serial.print(Val1&0xFF,BYTE);//explicitly mask the LSB using bitwise AND
Serial.println(Val1>>8,BYTE);//right-shift Val1 for MSB

Depending on the chip’s instruction set, a modulo (%) operation can actually be really expensive when compiled down (in terms of code size and clock cycles), when compared with bit-shifting/masking operations, which can usually be done in a single clock-cycle. On the Max side you can do a left-shift(< <8) on the MSB and add it to the LSB.
I believe there is a pretty good tutorial on bitwise operations on the Arduino site.
Best,
Andrew B.