We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
write to array (Read 1049 times)
write to array
Jan 21st, 2007, 3:41am
 
Hi all,

I found out that maxlink can only send a stream of numbers and not any entire arrays. In stead I would like to send each number of an array from max rather quickly after each other. How do I make a function that places incoming numbers at certain positions in an array? i.e I have 3 numbers: 4, 6, 8, comming in from max, and I want to write them into position 0, 1, 2, in an array in processing. I looked up arrays in the reference:

int[] numbers = new int[3];

int[] numbers = { 90, 150, 30 };

numbers[0] = 90;
numbers[1] = 150;
numbers[2] = 30;

- it looks like a tedious process to individually have to position each incoming number from max into an array. How do I automize this process?

Thank you,
GR
Re: write to array
Reply #1 - Jan 21st, 2007, 11:06am
 
The following code will let you add an arbitrary number of ints to an array in the order you get them.

Code:
int[] numbers=new int[0];

//recievedNumber is what you get from maxlink
numbers=append(numbers,recievedNumber);


Re: write to array
Reply #2 - Jan 23rd, 2007, 9:26pm
 
ok, I tried to include your suggestion above in my code. I get an ArrayIndexOutOfBoundException - here's my code:


import maxlink.*;

MaxLink link = new MaxLink(this, "message_board"); // get all messages from max link object - refer to object name
public float num1, num2, num3, num4, num5;

public int out1, out2, out3;
int posX, posY;

String oldMessage = "";
String message = "";
int messageCount = 0;
// array to contain numbers an array sent from max
float [] numbers = new float[0];
float recievedNumber;


void setup() {
 size(300,300);
 background(20);
 // inlets from max to processing
 link.declareInlet("num1");
 link.declareInlet("num2");
 link.declareInlet("num3");
 link.declareInlet("num4");
 //link.declareInlet("numbers");
 link.declareInlet("recievedNumber");
 // array from max
 //recievedNumber is what you get from maxlink
 numbers=append(numbers,recievedNumber);
 
}

void draw() {
 drawMessage();

 stroke(250);
 line(20, 300, 20, num1);
 stroke(250);
 line(30, 300, 30, num2);
 stroke(250);
 line(40, 300, 40, num3);
 
 ellipseColor();
 mouseAction();
 arrayFromMax();

}



// this method must be public - if a message number is different than previous, then notice it!
public void setMessage(String newMessage) {
 messageCount++;
 link.output(messageCount);
 oldMessage = message;
 message = newMessage;
}

void drawMessage() {
 background(20);

 if (oldMessage != "") text("previous message: " + oldMessage, 37, 80);
}

// these two circles will be white if box is checked in max and grey otherwise
void ellipseColor() {
 if(num4==1) {
   fill(255);
   noStroke();
   ellipse(200, 30, 20, 20);
 } else {
   fill(100);
   noStroke();
   ellipse(200, 30, 20, 20);
 }

if(num5==1) {
   fill(255);
   noStroke();
   ellipse(200, 57, 20, 20);
 } else {
   fill(100);
   noStroke();
   ellipse(200, 57, 20, 20);
 }
 
}

// mouseClick and x/y coordinates will be sent to maxlink object in max
void mouseAction() {
 posX=mouseX;
 posY=mouseY;
 
 if(mousePressed) {
   out1=1;
 } else {
  out1=0;
 }
 link.output(1,out1);
 
 noStroke();
 fill(255);
 rect(posX, posY, 20, 20);
 //println("posX");
 out2=posX;
 out3=posY;
 link.output(2,out2);
 link.output(3,out3);
}

void arrayFromMax() {
 fill(255);
 line(numbers[0], 50, numbers[1], 50);
}
Re: write to array
Reply #3 - Jan 23rd, 2007, 9:57pm
 
You're only putting one float into the array. I don't know how maxLink works, but this:

numbers=append(numbers,recievedNumber);

will only add a single float to the array. if you're receiving multiple, then you need to do a loop somewhere that'll read the newest values, and add them in.

The problem arises because:

  line(numbers[0], 50, numbers[1], 50);

is trying to access number[1] which doesn't exist yet, as you've only added one float, which is numbers[0].
Re: write to array
Reply #4 - Jan 24th, 2007, 6:26am
 
hmm, now I am just screwing around, but it seems like I can't really get to the data ... on the max side I send the array through the max object iter (iterates through the array and sends one number at a time to processing ...

Here's the code:

import maxlink.*;

MaxLink link = new MaxLink(this, "message_board"); // get all messages from max link object
public float num1, num2, num3, num4, num5;
public float [] numbers = new float[4];

public int out1, out2, out3;

public int flag1;

int posX, posY;

String oldMessage = "";
String message = "";
int messageCount = 0;
// array to contain numbers an array sent from max




void setup() {
 size(300,300);
 background(20);
 // inlets from max to processing
 link.declareInlet("num1");
 link.declareInlet("num2");
 link.declareInlet("num3");
 link.declareInlet("num4");
 link.declareInlet("num5");// array from max
 
 
 
 flag1 = -1;


 //recievedNumber is what you get from maxlink
 //numbers=append(numbers, recievedNumber);
 //}
 
}

void draw() {
 
 
 drawMessage();

 stroke(250);
 line(20, 300, 20, num1);
 stroke(250);
 line(30, 300, 30, num2);
 stroke(250);
 line(40, 300, 40, num3);
 

 
//  if (flag1 < 0) {
 //  flag1 = flag1 + 1;
//  }
//  if (flag1 > 3 | num5 > 300.0) {
//    flag1 = -1;
//  }

//      numbers[flag1] = num5;
//      print("array@"+flag1);
//      println(" = "+num5);


//    println("hello" + num5);

//  if(flag1 >= 0) {
//        numbers[flag1] = num5;
//        print("array@"+flag1);
//        println(" = "+num5);
//  }
 
 ellipseColor();
 mouseAction();
 arrayFromMax();
 
 



}



// this method must be public - if a message number is different than previous, then notice it!
public void setMessage(String newMessage) {
 messageCount++;
 link.output(messageCount);
 oldMessage = message;
 message = newMessage;
}

void drawMessage() {
 background(20);

 if (oldMessage != "") text("previous message: " + oldMessage, 37, 80);
}

// these two circles will be white if box is checked in max and grey otherwise
void ellipseColor() {
 if(num4==1) {
   fill(255);
   noStroke();
   ellipse(200, 30, 20, 20);
 } else {
   fill(100);
   noStroke();
   ellipse(200, 30, 20, 20);
 }

if(num5==1) {
   fill(255);
   noStroke();
   ellipse(200, 57, 20, 20);
 } else {
   fill(100);
   noStroke();
   ellipse(200, 57, 20, 20);
 }
 
}

// mouseClick and x/y coordinates will be sent to maxlink object in max
void mouseAction() {
 posX=mouseX;
 posY=mouseY;
 
 if(mousePressed) {
   out1=1;
 } else {
  out1=0;
 }
 link.output(1,out1);
 
 noStroke();
 fill(255);
 rect(posX, posY, 20, 20);
 out2=posX;
 out3=posY;
 link.output(2,out2);
 link.output(3,out3);
}

void arrayFromMax() {
 fill(255);
 line(numbers[0], 50, numbers[1], 50);
//    println(numbers);
}
Page Index Toggle Pages: 1