Forums > MaxMSP

Communication Max-Arduino

October 2, 2013 | 4:52 am

hey there,

currently i’m working with an hc-sr 04 ultrasonic sensor connected to an arduino leonardo for doing distance measurement. inside the arduino environment everything works perfectly fine.
this is the code:

#define trigPin 8
#define echoPin 7

void setup() {
Serial.begin (9600);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
}

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);

Serial.println ();
delay(1000);

}

now i need to get the distance values into max/msp.
yet, i’ve worked with this patch from here treating kind of the same topic.

http://cycling74.com/forums/topic/ping-to-arduino-to-max/

– Pasted Max Patch, click to expand. –

so running this patch tells me that i’m definately receiving data (the bang connected to the rightmost outlet of the serial object). but i’m not getting to receive a list of numbers. for now the patch is only to get something like a symbol e.g.
oh, and the select is because of this quote (from kind of the same topic here in the forum back in 2009): "Third, you are looking for a carriage return to process your list (13), and since your sketch isn’t providing the carriage return it may not be the best way to group your list. Many serial devices add a carriage return (all my bluetooth modems do), but again, you aren’t specifically passing that along in your sketch. You are passing a line feed (10) via Serial.println, so group the list that way."
i guess that makes sense. unfortunately zl group doesn’t print anything, even if its argument is 1.

so, i don’t know. i’d be glad to get some help here.

cheers
robert


October 2, 2013 | 6:21 am

Hi
I’m not at Max currently, but I would just delete the serial.println. The raw data will show up at the left outlet of [serial]. If not, the problem lies elsewhere,

Brendan


October 2, 2013 | 8:02 am

Yeah, first of all I would ditch the itoa and from symbol and zl group; these depend on how the serial data is formatted at the Arduino end. If you’re running the latest version of Arduino IDE just use Serial.write (blah blah) which auto-formats as a integer byte. I think. As far as pings and ultrasound go, no idea sorry.

Brendan


October 2, 2013 | 4:05 pm

hey,
thanks for the quick response.
i ditched the itoa, from symbol and the zl group and i replaced the serial.print (distance) with a serial.write (distance).

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.write(distance);

still it says i’m receiving something but not what exactly.
anyone else may have got a clue here?
that’d be awesome.
robert


October 2, 2013 | 9:21 pm

I don’t think that’s right…
Serial.write(distance) will treat "distance" as a byte, i.e. 0-255.
should be something like this:

Serial.print(distance);
Serial.write(10); // line feed character.
Then you look for "10" in Max as your parsing/end of packet character.

Distance, btw, should be declared as a float since you are dividing by 29.1.

I can’t open you patch above, but coming out of the serial object should be something like:

– Pasted Max Patch, click to expand. –

This should give you your value in max. Also check your baud rate on the serial object (should match your sketch).

db


October 4, 2013 | 12:04 pm

hey there,
it is working now thanks to puredata. i don’t know what the exact problem is with max/msp (and i don’t care anymore ;) ), but there got to be one as i didn’t change the arduino-code. it still is Serial.print(distance);
Serial.write(distance);
and it works perfectly fine. now i am about to send the data from pd to max.
so for all those who are looking for an easy way to do this: there is one though its a detour.

nevertheless: thank you! great forum, great community!
robert


October 4, 2013 | 12:07 pm

oh, i forgot: in pd use [comport] (baude rate as an argument) and as messages "devices" (shows you the available ports), "open #numberofport" (e.g. open 12) and "close" (for closing the port).
robert


October 5, 2013 | 9:38 am

Finally saw your patch was attached above (copy/paste didn’t work). Add a "zl iter 1" between the serial object and sel object. Especially given that you are polling the serial object every 20 ms, your data is being passed out the serial object as one "chunk". So it’s most likely never going to see "10" since it’s going to be a the end of the list. Add "zl iter 1" and it should work. The problem is most likely in your patch, not max. I have yet to run into a device I couldn’t communicate with in Max via serial…and there have been many.

David


Viewing 8 posts - 1 through 8 (of 8 total)