Forums > MaxMSP

arduino and max – how to get sensor data into max

April 16, 2012 | 8:13 am

Hi,

It is my first time with the arduino interface, and I would like to get sensor data from ALL the arduino analogue input pins into max. Have anyone of you worked with this before? Please see my unsuccessful try below:

I have some arduino code that supposedly should get sensor data from all anaologue inputs (sensor inputs) to max:

/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor

This example code is in the public domain.
*/

void setup() {
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
}

—–

On the other end of the line I have this max patch:

– Pasted Max Patch, click to expand. –

Right now I don’t seem to get the sensor data from the arduino into max – I wonder what the problem is?

Can anyone help me?

Thanks a lot,

Kaspar


April 16, 2012 | 10:42 am

Does the serial monitor in the Arduino show values? Then your connection works. In order to make it work in max, you need to close the serial monitor. Moreover, the values received from the serial object are ascii values that need to be converted using [atoi].


April 16, 2012 | 11:13 am

Hi
the code you posted will only read the 1st analogue input on the Arduino:

"Reads an analog input on pin 0, prints the result to the serial monitor"

You don’t mention which Arduino you have (i.e. number of inputs, software version) but let’s assume you have the Uno and Arduino 1.0 version software, you need the following code:

//Brendan McCloskey
//Read all 6 analogue inputs and send values to the serial port

int touchVal;
int pinArray[]={0, 1, 2, 3, 4, 5};//AN ARRAY TO STORE ANALOG PINS

void setup(){
Serial.begin(9600);
}

void loop(){
for(int x=0; x<6; x++){//CYCLE THRU ALL INPUTS
touchVal=analogRead(x);
touchVal=map(touchVal, Y, Z, 0, 250);//SEE BELOW
touchVal=constrain(touchVal, 0, 250);
Serial.write(x);//SEND AN INDENTIFIER FOR EACH UNIQUE INPUT READING
Serial.write(touchVal);//SEND THAT PIN’S VALUE; see Arduino.cc "Serial.write"
}
delay(5);
}
/*
IN THE CODE ABOVE YOU MUST REPLACE Y AND Z AT touchVal=map…. WITH THE MIN AND MAX VALUES YOU GET FROM YOUR SENSORS
*/

Then in Max you need:

[metro 50]

[serial 9600]

[zl group 2] (to package the identifier and it’s value together)

[route 0 1 2 3 4 5] (to unpack the 6 data streams]

I use this method all the time and it is without error, so make sure you’ve correctly followed the install/setup/test procedure outlined on the Arduino site.

Brendan


May 11, 2014 | 8:32 am

Hey Brendan,

Trying to find a solution for my issue to no avail at all! Have come across this thread and I think you might be able to help me…

Attached is a screenshot of my patch. My Arduino sketch is showing which pin/sensor (from a capacitive touch sensor) is being touched, but Max can only pick up 0, (I need to know if it’s Pin 1 / 2 etc.) Could you tell me if there some arguments that need to be added along my [zl] – [unpack] chain?

Thank you so much for your time.
J.

Attachments:
  1. Screen-Shot-2014-05-11-at-16.27.56

May 11, 2014 | 10:35 am

Hi
if you compare my code above, with the code you’re running for your setup – there’s a huge difference, and it pains me to admit that my Arduino chops are limited. The MPR and Wire libraries are a mystery to me too, but as far as I can tell from your other threadS on this topic, the Arduino code is the answer, because it’s not written explicitly for Max. It’s easier (for me) to parse a chain of bytes representing the actual sensor values, without having to deal with delimiters, punctuation and text strings, as in the example you give. It appears to be sending a fairly long string consisting of the sensor ID, a colon and then some text representing it state. The Wire library also appears to be doing some printing or writing too.

This would necessitate some hardcore string parsing in Max beyond me: [sprintf], [route], [match] maybe even javascript. Do a quick bit of research and see if you can change the code to say: here’s the sensor ID, here’s a 1 if it’s touched (or 0 if not).

Good luck


May 11, 2014 | 10:37 am

ps

in your screenshot [zl group] needs an integer argument; without one, I think it does nothing.

Have you tried the Arduino forum – there might some more betterer help available there.


May 11, 2014 | 10:45 am

Brendan,

Thank you so much for your reply. You’re right, I do think more on the Arduino end. I didn’t write the code, only tweaked it. The Arduino forum is a lot less helpful I’m afraid! – I think the best route for me is to painstakingly go through the Arduino code I am working with.

Very much appreciated,
Best,
J.


May 11, 2014 | 10:46 am

pps

While I normally abhor screenshots, why not post a small shot of your Arduino serial monitor, illustrating when you touch one of the sensors. You say this is working OK in Arduino but not Max, in your other thread. It might simply be a matter of changing the code to say Serial.write(1);
instead of Serial.print("touched");
and Serial.write(0);
instead of Serial.print("nottouched");

?


May 11, 2014 | 10:49 am

Just saw your reply.

Unless any more specific advice is forthcoming, I think the best thing is to just stick with Arduino for the time being until you can dictate exactly what the the code outputs. That way, you’re not scrambling around in the dark (as I am), trying to predict what will happen at the Max end.

Sorry not to be more help.

"Arduino_Noob_Meister"


May 11, 2014 | 10:52 am

pppppppps

Arduino has a really cool and helpful trick: if you right-click on any native function and select Find in Reference, this will take you to a quick helpfile tutorial thingy.


May 12, 2014 | 8:06 am

Hey Brendan,

Once again, thank you so much for your helpful replies. Attached is a screenshot of my Arduino’s serial monitor. You’re definitely right anyway, the code is not yet written to convey to data I need it to. The ‘Find Reference’ trick is also very helpful towards this end. I’m going to rummage around in my code for the next while and will let you know how I get on.

Thanks again Brendan,
J.

Attachments:
  1. Screen-Shot-2014-05-12-at-16.02.51

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