Handling multiple variables over serial from Max/MSP to Arduino

Hi,

I’m having trouble getting Arduino to handle multiple incoming variables via serial communication (from Max/MSP) and am in serious need of help! My project includes outputting “lists” of variables to the arduino, including an “identifier” [0 - 3], along with an integer that I aim to assign to a variable within Arduino.

I stumbled upon this thread concerning the exact same topic, however the OP never explained how he tackled the problem:

For those who know how to use Max, here is my problem:

I’m not too show how to format the information before sending it over serial.

And for the Arduino-ers:

How do I handle multiple corresponding variables to ensure the information is a) recognized, and b) stored for use.

Here is a sample of the code I’m using to “handle” the data:

if (Serial.available() > 1){ //if 2 bytes available in the Serial buffer…

for (int i=0; i<2; i++) { // for each byte
bufferArray[ i ] = Serial.read(); // put into array
output = 1; // show new data has been recieved
}
}

if (output != 0) // if new bytes have been recieved
{
rowNumber = bufferArray[0]; // store first byte as “identifier”
rowBinary = bufferArray[1]; // store second byte as “value”
output = 0;
}

if (rowNumber == 0){
ledRow1 = rowBinary;
}

if (rowNumber == 1){
ledRow2 = rowBinary;
}

if (rowNumber == 2){
ledRow3 = rowBinary;
}

if (rowNumber == 3){
ledRow4 = rowBinary;
}

Any help would be greatly appreciated!
Thanks,
Ben K

Last Edit: Slight change in code, thanks PaulS!

For those who are interested in helping and possibly need more information, I've posted the same topic on the Cycling 74 Forums, which include the Max/MSP code as well!

http://cycling74.com/forums/topic.php?id=38250#post-186189

if (Serial.available() > 1){                    //if 2 bytes available in the Serial buffer...
 
      for (int i=0; i<3; i++) {         // for each byte
       bufferArray[ i ] = Serial.read();        // put into array

If there are two bytes available, read all three of them. How’s that working for you?

I've changed the code so many times it's easy to miss little things like that!

Fixed, but still no luck. I wish it was as simple as something like that!

Thanks, Ben K

And just to clarify what data the Arduino is receiving over the serial:

Packets that are made up of two variables separated by a space : (X Y)

X = Between 0-3, and Y = Between 0-15.

Cheers, Ben K

And just to clarify what data the Arduino is receiving over the serial:

Packets that are made up of two variables separated by a space : (X Y)

X = Between 0-3, and
Y = Between 0-15.

if (output != 0)                    // if new bytes have been recieved
{               
   rowNumber = bufferArray[0];                // store first byte as "identifier"              
   rowBinary = bufferArray[1];         // store second byte as "value"
   output = 0;             
}

So, the X value (possibly an ASCII value) is being stored in rowNumber, and the space is being stored in rowBinary. Again, the somewhat facetious question, how’s that working for you?

Also, you said:

I’m not too show how to format the information before sending it over serial.

And yet you think that you know how to read the data that you don’t know how to send. Seems to me like you need to be certain what is being sent before you can write the reader code. Or maybe that’s just the way I’d do it.

This does appear to have been my problem, thanks for the excellent advice.

I've changed it around a little to correspond to how the data is formatted that is incoming and I appear to have some control over my data now. Just having trouble storing the variables into an array, but I can figure that out. I'm not an advanced programmer, as you can probably tell.

Thanks for the speedy help! Ben K

hi ben, did you figure it out? I'm having the same problem as you, would be great if you want to share the max + arduino code.

All the best, Lasse