Communicate Arduino and Max with simple serial

chen light's icon

Hi, I am a max beginner. I want to communicate Arduino and Max.
I know there are some library, Maxuino. Arduino2Max..etc.
But being a beginner, I want communicate simplest first.

I just use object "serial", and give it COM number and rate 9600.
My Arduino code is simple, too.

void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(1);
delay(100);
}

It will sent a number "1" to serial port.
And I use Max to read the serial.
But no matter I try, my max can't get any thing.
I already use other serial reader checked the serial port, the data Arduino send are correct.
So I think the problem is my max.

I don't know how can I do anymore.
Please tell me what mistake I do.
Best gratitude.

add my max file.

maxconnecter.maxpat
Max Patch
matteopennese's icon

Hi,
check out these good tutorials here http://cskonopka.github.io/arduivis/maxmodels

m

brendan mccloskey's icon

Hi
if you are using the latest version of the Arduino IDE, try replacing the Serial.println with the following: Serial.write random(0, 250); this is as basic as it gets

Max Patch
Copy patch and select New From Clipboard in Max.

then this patch should work. If not, then the problem lies elsewhere (COM port). I never liked messing about with all that ASCII conversion stuff ;)

Brendan

hz37's icon

It might be the com port, yes. Did you try [serial c 9600] and [serial b 9600] ??

Good luck!

First Last's icon

Hey try this, it should work just change the port to whatever port you are using, i used it to pick up light levels, obviously change the scaling too. here is the adjoining arduino code

/* Simple test of the functionality of the photo resistor

Connect the photoresistor one leg to pin 0, and pin to +5V
Connect a resistor (around 10k is a good value, higher
values gives higher readings) from pin 0 to GND. (see appendix of arduino notebook page 37 for schematics).

----------------------------------------------------

PhotoR 10K
+5 o---/\/\/--.--/\/\/---o GND
|
Pin 0 o-----------

----------------------------------------------------
*/

int lightPin = 0; //define a pin for Photo resistor
int ledPin=11; //define a pin for LED

void setup()
{
Serial.begin(9600); //Begin serial communcation
pinMode( ledPin, OUTPUT );
}

void loop()
{
Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
analogWrite(ledPin, analogRead(lightPin)/4); //send the value to the ledPin. Depending on value of resistor
//you have to divide the value. for example,
//with a 10k resistor divide the value by 2, for 100k resistor divide by 4.
delay(10); //short delay for faster response to light.
}

photo-arduino.maxpat
Max Patch
Tobias Rosenberger's icon

the first patch works. so either wrong serial port or you didn't close arduino ide before starting your max patch (serial communication only possible to one application at a time)

astraphl's icon

That was the hint I needed.. Thanks

Tori's icon

Thanks for the advice on this guys. I had this same issue and was able to solve the problem by using Serial.write in my Arduino code.

Brendan, do you (or anyone else) know why it needs to be Serial.write instead of Serial.println?

Thanks again :)

Tori's icon

This code contains both the Max and Arduino code that you need in order to communicate between Arduino and Max.
I've tested this code and it works well. There are also a couple of trouble shooting tips that are written into the Max patch.

Max Patch
Copy patch and select New From Clipboard in Max.

brendan mccloskey's icon

Hi Tori
thanks for sharing an annotated solution; so many people often forget to do this, well done.

The difference between .write and .print (afaik) is that .print can send a variety of 'formats' (scare quotes!): decimal, binary, octal etc, but commonly as a stream of ASCII-encoded numbers representing your sensor's numerical values; so no limit on the range of numbers to send/receive. Serial,write, however, sends the values as an 8-bit data byte, so no values larger than 255 - unless you use bitshifting and byte manipulation in Max. It's eminently possible, and it was the method I used for greater resolution than 0~255, until I encountered Arduivis. Incidentally, Serial,print(x, BYTE) is now deprecated in the Arduino IDE, having been replaced with Serial.write.

HTH
Brendan

ps, on this and similar threads I noticed how I said that I don't like to use .print from Arduino, because it involved manipulation of ASCII and Unicode formats; and now I wouldn't use anything else in Max.

Tomayto, tomahto . . .

Tori's icon

Amazing! Thanks so much for that extremely thorough explanation Brendan--it makes total sense. I'll be sure to use Serial.write from now on :)

brendan mccloskey's icon

PS I hope I didn't suggest that you SHOULD always use Serial.write - it depends entirely upon your own personal needs -read these thoroughly:

And you can then make a more informed decision
;)

Tupac Torres Bartsch's icon

hm

the troubleshooting methods here
didnt work in my case

the serial object in max 7 is saying

''works only with ports and devices supported by the standard serial driver. It does not work with USB ports and devices, unless a USB to Serial adaptor is connected.''

whats the actual difference?
does USB ports and devices mean the specific tool drivers?

there is a chinese ATmega Driver to understand my Nano.
could this be the problem?

or any else ideas?

Robin Parmar's icon

It could indeed be that the driver for the nano clone is not properly emulating a serial connection. That would be odd, but easy to test. Just try some other programme and see if it connects to the Nano. Does the serial monitor in the Arduino UI work?

You should also ensure the entire Arduino interface and any other software is closed before testing with Max. This ensures any serial connection is closed and that channel is free for Max to use.

P.S. In these cases I would recommend starting a new thread rather than revisiting one from a year ago.

Tupac Torres Bartsch's icon

thanks robin.

yes all other software was closed.
all communication is working fine in the arduino monitor

i guess its really the driver
yesterday i started a new top
this chinese atmega chips are quite popular maybe someone found a solution
if i find something. its up in web.

MMa's icon

Thank you very nice!

Simone Grande's icon

Tobias Rosemberg,
you saved my life.

Robin Morabito's icon

Tori you are amazing and your simple patch saved me hours of pain & swearing. THANK YOU SO MUCH.

John Horowitz's icon

Wow thanks for this thread! I finally got the connection Kit to recognize my fsr pad but I have a new problem.
The connection kit Analog connections don't map to drum cells, only faders, knobs and things like that.
How do I get the fsr readings to trigger drum rack cells in Ableton Live?