Can't read Nunchuk data

I have got a running wired nunchuk to set postion and parameters of my panorama robot

next step should be to take a wireless nunchuk to get rid of the cable

i am trying to connect it with my arduino but without success. even when nothing is connected the wiring functions return values like

get
read:
0100:read Z
get
read:
0100:read Z

get

read:

0100:read

this is my sketch:
has anybody an idea what i am doing wrong?

#include <SoftwareSerial.h>
#include <Wire.h>

// NUNCHUK (Pins: A4 green, A5 yellow
static uint8_t nunchuck_buf[6]; // array to store nunchuck data
// nunchuck X Y
int NUN_X;
int NUN_Y;
// nunchuk buttons
int NUN_C;
int NUN_Z;

//-------------------------
void setup() {

// init nunchuck
Serial.begin(19200);
nunchuck_init(); // send the initilization handshake
delay(200);
//nunchuck_zero();
nunchuck_get_data();
delay(200);
Serial.println(“Setup done”);

}
//----------------------
void get_params()
{

while(1)
{
//nunchuck_zero();
Serial.println("get ");
delay(200);
nunchuck_read();

if (NUN_C == 1) Serial.println("C ");
if (NUN_Z == 1) Serial.println("Z ");
}

delay(1000);
Serial.println(“DONE”);
delay(200);
}

//
//-------------------------------------------------------------
//
void loop()
{
// get paramters via nunchuck
get_params();

Serial.println(“XXX DONE XXX”);
delay(1000);
}

//----------------------------------------------------------------------
//
// Nunchuck functions
//
// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we’re talking to it
void nunchuck_init()
{
Wire.begin(); // join i2c bus as master
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.send(0x40); // sends memory address
Wire.send(0x00); // sends sent a zero.
Wire.endTransmission(); // stop transmitting
}
//---------
void nunchuck_send_request() // Send a request for data to the nunchuck
{
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.send(0x00); // sends one byte
Wire.endTransmission(); // stop transmitting
}
//---------
int nunchuck_get_data() // Receive data back from the nunchuck,
{
int cnt=0;
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.receive());
cnt++;
}
nunchuck_send_request(); // send request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
//---------
void nunchuck_read()
{
NUN_C = 0;
NUN_Z = 0;
NUN_X = 0;
NUN_Y = 0;

while (nunchuck_get_data() == 0);

NUN_C = nunchuck_cbutton();
NUN_Z = nunchuck_zbutton();
NUN_X = nunchuck_joyx();
NUN_Y = nunchuck_joyy();
Serial.println(“read:”);
Serial.print(NUN_C,DEC);
Serial.print(NUN_Z,DEC);
Serial.print(NUN_X,DEC);
Serial.print(NUN_Y,DEC);
Serial.print(":read ");
delay(1000);
}

//---------
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
// returns zbutton state: 1=pressed, 0=notpressed
int nunchuck_zbutton()
{
return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1; // voodoo
}

// returns zbutton state: 1=pressed, 0=notpressed
int nunchuck_cbutton()
{
return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1; // voodoo
}

// returns mapped value of x-axis joystick
int nunchuck_joyx()
{
int r;
r = nunchuck_buf[0];
return nunxymap(r);
}

// returns mapped value of y-axis joystick
int nunchuck_joyy()
{
int r;
r = nunchuck_buf[1];
return nunxymap(r);
}

// map val to a range of -1, 0, 1
int nunxymap(int val)
{
int r1;

r1 = 0;
// map r1 to -1, 0, 1
if ( val < 10) r1 = -1;
else if ( val > 240) r1 = 1;
if (NUN_C == 1) r1 *= 10; // x, y * 10, if C pressed
return r1;
}
//--------

Maybe a dumb question but worth trying : did you connect the VCC and GND cables on the nunchuk ? I made this mistake a few times, they were plugged to pins on the arduino, but I forgot to initialize the pins so no power to the nunchuk.

yes, i connected them, but without initializing the pins - how do I do that ?

and the sketch is pretty well running with the wired version of the nunchuk

So you're already on the wireless version ? How is the nunchuk connected to the arduino ? Bluetooth ? You have a dongle ?

Please explain the hardware side a bit more...

the wireless version has 2.4ghz transmission between nunchuk (2 AAA-batteries inside) and the transmitter-plug (just google pics with "nunchuk wireless snakebyte" or look at amazon for "nunchuk wireless")

to connect it with the arduino i made an adapter similiar to todbots wiichuck from pcb-material

As far as I can tell, you used the nunchuk library from todbot in your sketch. If you used exactly the same layout as supplied by todbot in his examples for the wiichuck adapter, you’re missing some lines in your code, and those lines are for powering the device.

You should replace your nunchuck_init function with this one

void nunchuck_init() {
    DDRC |= _BV(PC3) | _BV(PC2);   // make outputs
    PORTC &=~ _BV(PC2);            // ground pin
    PORTC |=  _BV(PC3);            // power pin
    delay(100);                    // wait for things to stabilize 

    Wire.begin();                  // join i2c bus as master
    Wire.beginTransmission(0x52);  // transmit to device 0x52
    Wire.send(0x40);               // sends memory address
    Wire.send(0x00);               // sends sent a zero.  
    Wire.endTransmission();        // stop transmitting
}

By the way, this snakebyte device seems to be nice… Might try it some day, a nice way to have some wireless controls, for a tilt pan camera for exemple ^^

You never complete the loop() function. This does not give the Arduino a chance to do any of the other stuff it should.

grumpymike : do you talk about the call to get_params ? It seems weird that you have a while(1) in your get_params... won't that loop forever ?

And why do you need the SoftwareSerial library ? It's not needed to talk to the nunchuck, as it's done over I2C, not serial.

do you talk about the call to get_params

Yes, as you say it will loop forever and so never complete the loop()
That’s not good. :frowning:

@melka thanks, I will try this evening to putin your init-additions

thats why my code looks strange: I cutted it out of a runnung programm which first set some params for an panorama robot device

@grumpymike SoftwareSerial is to show the settings on a serial LCD I stay in loop until all settings are done

the while(1) was just to get anything usable out of the nunchuk ... endless