how can i split the output of zl group object? (creating a MIDI Theremin)

The Rumblist's icon

Hello to all...

I'm using a modified version of the serial communication tutorial patch to read the serial input from an Arduino - this is sending two streams of numbers from two IR sensors. When i click on the Print Grouped option in the patch (highlighted in red) i get a readout in the max window of each sensor's value in turn, e.g. 50 41 53 on one line then 49 54 on the next line. My question is how can i split the numbers as they appear in the max window, i.e. in turn? I would like one list of numbers to control the pitch and the other stream to control velocity.

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

Thanks to anyone who can help.

The Rumblist's icon

Anybody able to help with this at all???

Anthony Palomba's icon

I don't have an Arduino, but if all the data is coming from serial
port, how can you distinguish the data from one sensor versus that of
the other?

pdelges's icon

@Anthony: depending on the software running on the arduino, this is not a problem: the short way is to send data1 data2 data1 data2... alternately, and split the data stream after [serial].
But as I never reallly trusted this, I prefer to send a string like data1_data2#data1_data2#... (or 1_data1#2_data2#1_data1#2_data2, or whaterver) so I'm sure to get the right pairs of data.

You should get an arduino, it's fun to program! :-)

The Rumblist's icon

Thanks for replying guys.....

@Patrick....please would you be able to show me where i would insert the data1_data2#data1_data2#...sorry to sound like a noob, but i have only just started getting into the Arduino language and am finding it a stretch! :-)

Here's the program i have running on the Arduino....(i'm sorry its long and convoluted, but i dont know a more elegant way!!)

Many thanks.

(taken from the Analog/Smoothing example)

const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = 0;

const int numReadings2 = 10;

int readings2[numReadings2]; // the readings from the analog input
int index2 = 0; // the index of the current reading
int total2 = 0; // the running total
int average2 = 0; // the average

int inputPin2 = 1;

void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

// initialize all the readings to 0:
for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++)
readings2[thisReading2] = 0;
}

void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;

// calculate the average:
average = total / numReadings;

average = map(average, 0, 700, 0, 255);
//adjust the value 0 to 900 to
//span 0 to 255

average = constrain(average, 0, 255);//make sure the
//value is betwween
//0 and 255

// send it to the computer (as ASCII digits)
Serial.println(average, DEC);

// subtract the last reading:
total2= total2 - readings2[index2];
// read from the sensor:
readings2[index2] = analogRead(inputPin2);
// add the reading to the total:
total2= total2 + readings2[index2];
// advance to the next position in the array:
index2 = index2 + 1;

// if we're at the end of the array...
if (index2 >= numReadings2)
// ...wrap around to the beginning:
index2 = 0;

// calculate the average:
average2 = total2 / numReadings2;

average2 = map(average2, 0, 700, 0, 255);
//adjust the value 0 to 900 to
//span 0 to 255

average2 = constrain(average2, 0, 255);//make sure the
//value is betwween
//0 and 255

// send it to the computer (as ASCII digits)
Serial.println(average2, DEC);
}

pdelges's icon

Sorry, I don't have much time now, so what I write is unverified.

Instead of your 2 Serial.println() lines, you could put, at the end of the loop() something like:

Serial.print(average1);
Serial.print("_");
Serial.printl(average2);
Serial.println();

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

and parse serial's output with something like:

Anyway, there are many ways to do that, that's part of the fun.

The Rumblist's icon

Thats wonderful Patrick...many many thanks! With a bit of tinkering i now have the patch working exactly how i wanted it to, with one sensor controlling volume and the other controlling pitch! (see below) :-) Wasnt sure how to split the serial stream, but that addition of the 95 to the Sel object in the patch you wrote makes total sense now!

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

You made my day!!! Thank you!!!