arduino and max – how to get sensor data into max

Apr 16, 2012 at 8:13am

arduino and max – how to get sensor data into max

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

#63001
Apr 16, 2012 at 10:42am

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].

#227366
Apr 16, 2012 at 11:13am

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

#227367

You must be logged in to reply to this topic.