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.
IndexProgramming Questions & HelpPrograms › Beginner help: can I get these 2 to work together
Page Index Toggle Pages: 1
Beginner help: can I get these 2 to work together? (Read 1216 times)
Beginner help: can I get these 2 to work together?
Dec 2nd, 2009, 1:38pm
 
I've got two programs that I'm trying to combine.  One program interprets data from a Wii nunchuk -- I've tested it in Processing and it works perfect. The other program is actually a simple graphic program from the "Learning Processing" book (the orange one).  It is example 9-8: A snake following the mouse(pg 152).  What I'd like to do is rather than have the snake follow the mouse, I want it to follow the movements of XYZ from the Wii nunchuck. I've only studied a tiiiiny bit in Java programming, so I feel like I understand the code a bit, but not nearly enough to get this new program up and running. Is anyone able to offer support?  I'll post all three codes below: Wii, snake, and the mess I made trying to get them to work together!  

This is the code for the Wii nunchuck:

Code:

/**
* this sketch displays all of the nunchuck inputs and a converted pitch and roll
*/

//float xmag, ymag = 0;
//float newXmag, newYmag = 0;
int sensorCount = 9; // number of values to expect

import processing.serial.*;
Serial myPort; // The serial port

int BAUDRATE = 115200;
char DELIM = ','; // the delimeter for parsing incoming data

void setup()
{
size(720, 480, P3D);
//noStroke();
colorMode(RGB, 1);
myPort = new Serial(this, Serial.list()[0], BAUDRATE);
// clear the serial buffer:
myPort.clear();

}
float x, z;

void draw()
{
background(0.5, 0.5, 0.45);

//BOX
pushMatrix();
translate(width/2 + sensorValues[5] * 2, height/2 + sensorValues[6] * 2, 0);
z = sensorValues[0] / 180 * PI ;
x = sensorValues[1] / 180 * PI;
rotateZ(z);
rotateX(x);
fill(255, 255, 255);
box(40, 70, 100);


//SPHERE
popMatrix();
pushMatrix();
translate(width/2 + sensorValues[2], 400 + -1 * sensorValues[4], -100 + sensorValues[3]); //this is where I am grabbing the accelerometer info from
fill(255, 0, 0);
sphere(50);
popMatrix();

fill(0, 0, 255);
if (sensorValues[7]==0) { // blue box button press
rect(10, 10, 40, 40);
}
fill(0, 255, 0);
if (sensorValues[8]==0) { //green box button press
rect(60, 10, 40, 40);
}
}
float[] sensorValues = new float[sensorCount]; // array to hold the incoming values

void serialEvent(Serial myPort) {
// read incoming data until you get a newline:
String serialString = myPort.readStringUntil('\n');
// if the read data is a real string, parse it:

if (serialString != null) {
//println(serialString);
//println(serialString.charAt(serialString.length()-3));
// println(serialString.charAt(serialString.length()-2));
// split it into substrings on the DELIM character:
String[] numbers = split(serialString, DELIM);
// convert each subastring into an int
if (numbers.length == sensorCount) {
for (int i = 0; i < numbers.length; i++) {
// make sure you're only reading as many numbers as
// you can fit in the array:
if (i <= sensorCount) {
// trim off any whitespace from the substring:
numbers[i] = trim(numbers[i]);
sensorValues[i] = float(numbers[i]);
}
// Things we don't handle in particular can get output to the text window
//print(serialString);
}
}
}
}



This is the code for the "snake following the mouse":

Code:

//x and y positions
int [] xpos = new int[50];
int [] ypos = new int[50];

void setup() {
size(720, 480);
smooth();

//Initialize
for(int i = 0; i < xpos.length; i++) {
xpos[i] = 0;
ypos[i] = 0;
}
}

void draw() {
background(255);

//shift array values
for(int i = 0; i < xpos.length-1; i++) {
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}

//new location
xpos[xpos.length-1] = mouseX;
ypos[xpos.length-1] = mouseY;

//draw everything
for(int i = 0; i < xpos.length; i++) {
noStroke();
fill(255-i*5);
ellipse(xpos[i],ypos[i],i,i);
}
}




I'm going to post my attempt at combining them in the following post due to lack of space!
Re: Beginner help: can I get these 2 to work together?
Reply #1 - Dec 2nd, 2009, 1:40pm
 
This is the mess I've made trying to get them to work together.  I think my main error is trying to cut and paste the two programs together, so the logic doesnt flow.

Code:



float sensorCount = 9; // number of values to expect

import processing.serial.*;
Serial myPort; // The serial port

int BAUDRATE = 115200;
char DELIM = ','; // the delimeter for parsing incoming data

int [] xpos = new int[50];
int [] ypos = new int[50];
int [] zpos = new int[50];

void setup() {
size(720, 480);
//noStroke();
smooth();
colorMode(RGB, 1);
myPort = new Serial(this, Serial.list()[0], BAUDRATE);
// clear the serial buffer:
myPort.clear();

//Initialize
for(int i = 0; i < xpos.length; i++) {
xpos[i] = 0;
ypos[i] = 0;
zpos[i] = 0;
}
}

float x, z;

void draw() {
background(0);

//shift array values
for(int i = 0; i < xpos.length-1; i++) {
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
zpos[i] = zpos[i+1];
}

//new location based on accelerometer reading
xpos[xpos.length-1] = sensorValues[2];
zpos[xpos.length-1] = sensorValues[4];
ypos[xpos.length-1] = sensorValues[3];
z = sensorValues[0] / 180 * PI;
x = sensorValues[1] / 180 * PI;

//button presses
fill(0, 0, 255);
if (sensorValues[7]==0) { // blue box button press
rect(10, 10, 40, 40);
}
fill(0, 255, 0);
if (sensorValues[8]==0) { //green box button press
rect(60, 10, 40, 40);
}
}

float[] sensorValues = new float[sensorCount]; // array to hold the incoming values

void serialEvent(Serial myPort) {
// read incoming data until you get a newline:
String serialString = myPort.readStringUntil('\n');
// if the read data is a real string, parse it:

if (serialString != null) {
//println(serialString);
//println(serialString.charAt(serialString.length()-3));
// println(serialString.charAt(serialString.length()-2));
// split it into substrings on the DELIM character:
String[] numbers = split(serialString, DELIM);
// convert each subastring into an int
if (numbers.length == sensorCount) {
for (int i = 0; i < numbers.length; i++) {
// make sure you're only reading as many numbers as
// you can fit in the array:
if (i <= sensorCount) {
// trim off any whitespace from the substring:
numbers[i] = trim(numbers[i]);
sensorValues[i] = float(numbers[i]);
}
// Things we don't handle in particular can get output to the text window
//print(serialString);
}
}
}
}

Re: Beginner help: can I get these 2 to work together?
Reply #2 - Dec 2nd, 2009, 3:58pm
 
My first suggestion: forget about any 'z' data coming from the nunchuck: you're not likely to be able to use it in a meaningful way.  And in actual fact you only need the x/y data to do the equivalent of controlling the mouse position.  With the snake code you'd simply need to replace the mouseX/mouseY with data from the nunchuck; though you'd also need to scale the incoming data to the screen.

Are you using the nunchuck_funcs.h code?  If so I'll post some example code.  I don't have my Arduino/nunchuck rigged up but I'm pretty sure this is the last working version I wrote:

ARDUINO
Code:
/*
* Adapted from:
* WiiChuckDemo --
*
* 2008 Tod E. Kurt, http://thingm.com/
*
*/

#include <Wire.h>
#include "nunchuck_funcs.h"

byte accx,accy,accz,zbut,cbut,joyx,joyy;

int ledPin = 13;

void setup() {
   Serial.begin(19200);
   
   while (Serial.available() <= 0) {
     //Serial.print('\r', BYTE);
     // or maybe:
     Serial.print('\n');
     delay(300);
   }

   pinMode(ledPin, OUTPUT);      
   nunchuck_setpowerpins();
   nunchuck_init(); // send the initilization handshake
}

void loop() {
 if(Serial.available()>0) {
   
       nunchuck_get_data();
       
       zbut = nunchuck_zbutton();
       cbut = nunchuck_cbutton();
     
       joyx = nunchuck_joyx();
       joyy = nunchuck_joyy();

       accx  = nunchuck_accelx(); // ranges from approx 70 - 182
       accy  = nunchuck_accely(); //
       accz  = nunchuck_accelz();
       
       Serial.print(zbut,DEC);
       Serial.print(",");
       Serial.print(cbut,DEC);
       Serial.print(",");
       Serial.print(accx,DEC);
       Serial.print(",");
       Serial.print(accy,DEC);
       Serial.print(",");
       Serial.print(accz,DEC);
       Serial.print(",");
       Serial.print(joyx,DEC);
       Serial.print(",");
       Serial.print(joyy,DEC);
       Serial.print("\n");
       
       if(zbut){
         digitalWrite(ledPin,HIGH);
       }
       else {
         digitalWrite(ledPin,LOW);
       }
 }
}


PROCESSING:
Code:
import processing.serial.*;

boolean connected = false;
int linefeed = 10;
Serial myPort;

float x,y;
int btnZ, btnC;
float accX, accY, joyX, joyY;

float centreX, centreY;


void setup() {
 size(400,425);
 println(Serial.list());

 myPort = new Serial(this, Serial.list()[1],9600);
 myPort.bufferUntil(linefeed);

 centreX = width/2;
 centreY = 5+(height/2);
 x = centreX;
 y = centreY;
 
 btnZ = 0;
 btnC = 0;
 
 
}

void draw() {
 background(0);  
 fill(150,0,0);
 ellipseMode(CENTER);
 ellipse(x,y,40,40);
 
 if(btnZ==1) {
   x = centreX + ((accX - 120)/100) * centreX * 2;
   y = centreY - ((accY - 120)/100) * centreX * 2;
   fill(0,150,0);
 }
 else {
   x = centreX + ((joyX - 135)/ 200) *centreX*2;
   y= centreY - ((joyY - 129)/200) * centreX*2;  
 }
 rect(0,0,width/2,25);
 
 if(btnC==1) {
   fill(0,150,0);
 }
   else {
   fill(150,0,0);
 }
 rect(width/2,0,width/2,25);  

   
 if(connected ==false) {
   myPort.write('\r');
 }
   
}

void serialEvent(Serial myPort) {
 println("event");
 if (connected == false) {
   myPort.clear();
   connected = true;
   myPort.write('\r');  
 }
   
 String myString = myPort.readStringUntil(linefeed);
 
 if (myString != null) {
   myString = trim(myString);
   int sensors[] = int(split(myString, ','));
   for(int i=0; i<sensors.length; i++) {
     btnZ = sensors[0];
     btnC = sensors[1];
     if (btnZ == 1) {
       accX = sensors[2];  
       accY = sensors[3];
     }
     else {
       joyX = sensors[5];  
       joyY = sensors[6];
     }
   //print(i + ":  " + sensors[i]  + "\t");
   }
 }
 myPort.write('\r');
       
}


Re: Beginner help: can I get these 2 to work together?
Reply #3 - Dec 12th, 2009, 9:56am
 
Hm, when I run the code that you posted, it doesnt appear to work.  I get a black box with two red rectangles at the top, and none of it is responsive to the nunchuck movements, button clicks, etc.

Any additional tips on getting your posted code up and running? Is there something I am overlooking?
Re: Beginner help: can I get these 2 to work together?
Reply #4 - Dec 12th, 2009, 12:23pm
 
also, I'm getting an error in Arduino.  It can seem to find the nunchuck_funcs.h library, which I have sitting in the same folder as the sketch.  Is this the correct place for it?
Re: Beginner help: can I get these 2 to work together?
Reply #5 - Dec 13th, 2009, 2:55am
 
Well - I've got nunchuck_funcs.h on a separate tab in the Arduino programme - but IIRC that should be equivalent to having the file in the folder.  Obviously if the programme isn't working on the Arduino you're not going to see much in Processing; but when it is you should make sure Processing is listening at the right port.  You may need to change this line:

Code:
myPort = new Serial(this, Serial.list()[0], BAUDRATE); 



Serial.list()[0] is an array of ports so you can change the index to the appropriate port in the list (if you're not sure which one IIRC you can just iterate through the list and println each one).
Page Index Toggle Pages: 1