Hi! I’m new to Processing and I’ve been trying to modify this code here (http://www.instructables.com/id/Control-a-RepStrap-RepRap-CNCCartesian-Bot-usi/step20/3D-Bouncing-Ball-Processing/) to work with theWiiChuckClass (for the Wii Nunchuk) using Arduino.
I’ve managed to get the ball moving around using the Nunchuk but when I move left the ball goes right and vice versa. Also, the ball’s movements are restricted and I don’t know which parts of the code to change to make the ball move anywhere :S
I was wondering if anyone here could give me any advice or suggestions on how to change the code below.
Thanks for your time
/////////////////// *********PROCESSING CODE ONLY*********
/**
NOTE: 0,0,0 - top left corner (facing the computer monitor)
negative Z axis points 'into' the computer monitor
positive Y axis points down
positive X axis points to the right
*/
float xmag, ymag = 0;
float newXmag, newYmag = 0;
int sensorCount = 5; // 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 size = 40; // Width of the shape
float xpos, ypos, zpos; // Starting position of shape
float depth;
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
int zdirection = 1; //front or back
void setup()
{
size(1024, 768, P3D);
noStroke();
frameRate(60);
smooth();
// Set the starting position of the shape
xpos = width/2;
ypos = height/2;
zpos = -height/2; //note that Zaxis goes 'into' the screen
depth = -height;
myPort = new Serial(this, Serial.list()[1], BAUDRATE);
// clear the serial buffer:
myPort.clear();
}
float x, z;
void draw()
{
background(102);
lights();
//glass box //note: line(x1, y1, z1, x2, y2, z2)
stroke(255);
//back
line(0,0,depth, width,0,depth);
line(0,height,depth, width,height,depth);
line(0,0,depth, 0,height,depth);
line(width,0,depth, width,height,depth);
//corners
line(0,0,0, 0,0,depth);
line(0,height,0, 0,height,depth);
line(width,0,0, width,0,depth);
line(width,height,0, width,height,depth);
newXmag = mouseX/float(width) * TWO_PI;
newYmag = mouseY/float(height) * TWO_PI;
float diff = xmag-newXmag;
if (abs(diff) > 0.01) {
xmag -= diff/4.0;
}
diff = ymag-newYmag;
if (abs(diff) > 0.01) {
ymag -= diff/4.0;
}
// if ((sensorValues[1] > 15) && (sensorValues[1] < 165)) {
z = sensorValues[0] / 180 * PI ;
x = sensorValues[1] / 180 * PI;
// }
rotateZ(z);
rotateX(x);
// Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > width-size || xpos < 0) {
xdirection *= -1;
}
if (ypos > height-size || ypos < 0) {
ydirection *= -1;
}
if (zpos < -height-size || zpos > 0) { //note that Zaxis goes 'into' the screen
zdirection *= -1;
}
// Draw the shape
lights();
translate(xpos, ypos, zpos);
sphere(size);
}
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);
}
}
}
}