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');
}