Sending two different data from Arduino to Max

R_Gol's icon

Hi,
I have an Arduino patch that is receiving a data from an UltraSonic sensor and another patch that receiving a data from a photoResistor cell. Right now I manage to receive those datas separately into Max msp via the Serial object. I was wondering how can I have both data sending from arduino into two different Serial ports in Max patch ?

Thanks for any help

Source Audio's icon

You should form Your questions more understandable.

R_Gol's icon

Sorry for my wording.
I hope this one will be clearer:

I try to send two streams of data from Arduino to max.
One stream of data is from an ultrasonic sensor and the other from a photo resistor cell.
I got no issue sending each stream of data separately from Arduino to Max using Serial.print in Arduino and Serial object in Max.
The problem for me is to understand how to send two streams of data from Arduino into Max and have them independent from each other inside Max.

Source Audio's icon

option 1:
Serial.print(sensor1); Serial.print(" "); Serial.println(sensor2);
option 2:
Serial.print("A "); Serial.println(sensor1); Serial.print("B "); Serial.println(sensor2);
-----
Which is better depends on ammount of data.
Prepending a letter as ID and sending only sensor that changed,
would reduce serial traffic.
Forming a list like in option 1 would allways send all sensors state,
no matter if they changed or not.

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

R_Gol's icon

Thank you! That was exactly what I needed!
I have another question within this topic -

For the moment I successfully receiving and processing multiple data from Arduino in Max patch.
Now I try to understand how can I sending back data within the same patch from Max to be received in Arduino. ( I try to send 0 and 1 to switch led on and off in Arduino.
Thanks!

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

Source Audio's icon

You need to send ASCII to arduino.
one would use atoi for that.
In this simple case (just turn 1 led on / off)
this would be ok :

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

in the patch is arduino code to toggle or fade LED
on pin 11, which should be pwm capable on most boards

R_Gol's icon

Great! thanks for your kind explanations!