Nunchuk analogXX not being detected (possible coding mistake)

This is my first arduino project, I'm trying to connect a WII Nunchuk to my laptop to use as mouse. I've been reading and trying to find solutions to my problem but I've hit a brick wall and need some help.

The article I'm following is this:

The Nunchuk I'm using is this:

http://www.ebay.co.uk/itm/New-1pc-White-Nunchuck-Nunchuk-Controller-For-Nintendo-Wii-Console-High-quality-/251719358479?pt=LH_DefaultDomain_3&hash=item3a9ba4980f

The arduino detected the nunchuk and the python script worked, the nunchuk became the mouse.

However, the cursor was going nuts across the screen and the buttons weren't working. The serial monitor showed the 7 collums (2 for the analog stick, 3 for the accelerometer and 2 for the buttons), however the numbers changes were not in accordance to the movements.

First thing I did was to redefine the center positions in the Python scrypt (NunchukMouse_release). This made the cursor steady. However, the buttons still didnt work and analogXX didnt work either.

Some reading and closer inspection of the Serial Monitor showed the 7 collums as:

  1. responding to analogYY as it should
  2. responding to accelXX when it should be responding to analogXX
    3,4 responding to accelYY and accellZZ
  3. always-changing four digit number but not responding to movement
    6,7 not responding C&Z buttons

I inspected the original .cpp file:

/*
 * ArduinoNunchuk.cpp - Improved Wii Nunchuk library for Arduino
 *
 * Copyright 2011-2013 Gabriel Bianconi, http://www.gabrielbianconi.com/
 *
 * Project URL: http://www.gabrielbianconi.com/projects/arduinonunchuk/
 *
 * Based on the following resources:
 *   http://www.windmeadow.com/node/42
 *   http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
 *   http://wiibrew.org/wiki/Wiimote/Extension_Controllers
 *
 */

#include <Arduino.h>
#include <Wire.h>
#include "ArduinoNunchuk.h"

#define ADDRESS 0x52

void ArduinoNunchuk::init()
{
  Wire.begin();

  ArduinoNunchuk::_sendByte(0x55, 0xF0);
  ArduinoNunchuk::_sendByte(0x00, 0xFB);

  ArduinoNunchuk::update();
}

void ArduinoNunchuk::update()
{
  int count = 0;
  int values[6];

  Wire.requestFrom(ADDRESS, 6);

  while(Wire.available())
  {
    values[count] = Wire.read();
    count++;
  }

  ArduinoNunchuk::analogX = values[0];
  ArduinoNunchuk::analogY = values[1];
  ArduinoNunchuk::accelX = (values[2] << 2) | ((values[5] >> 2) & 3);
  ArduinoNunchuk::accelY = (values[3] << 2) | ((values[5] >> 4) & 3);
  ArduinoNunchuk::accelZ = (values[4] << 2) | ((values[5] >> 6) & 3);
  ArduinoNunchuk::zButton = !((values[5] >> 1) & 1);
  ArduinoNunchuk::cButton = !((values[5] >> 0) & 1);

  ArduinoNunchuk::_sendByte(0x00, 0x00);
}

void ArduinoNunchuk::_sendByte(byte data, byte location)
{
  Wire.beginTransmission(ADDRESS);

  Wire.write(location);
  Wire.write(data);

  Wire.endTransmission();

  delay(10);
}

And made these changes to this section:

ArduinoNunchuk::analogX = values[0];
  ArduinoNunchuk::analogY = values[0];
  ArduinoNunchuk::accelX = (values[1] << 2) | ((values[5] >> 2) & 3);
  ArduinoNunchuk::accelY = (values[2] << 2) | ((values[5] >> 4) & 3);
  ArduinoNunchuk::accelZ = (values[3] << 2) | ((values[5] >> 6) & 3);
  ArduinoNunchuk::zButton = !((values[4] >> 1) & 1);
  ArduinoNunchuk::cButton = !((values[4] >> 0) & 1);

  ArduinoNunchuk::_sendByte(0x00, 0x00);

I think what I did was redefine the how the arduino reads the output of the nunchuk. Now the 7 collums look like:

  1. responding to analogYY
  2. responding to analogYY
    3,4,5 responding to accelXX,YY,ZZ
    6,7 responding to C&Z buttons

I guess the bytes coming off the nunchuk weren't being read in the correct order but I still dont have analogXX response and I dont know which value to attribute....

Any guidance would be appreciated!

Sources:

http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
https://web.archive.org/web/20140325144624/http://www.windmeadow.com/node/42
https://web.archive.org/web/20090423001134/http://www.wiili.org/index.php/Wiimote/Extension_Controllers/Nunchuk

This looks wrong:

ArduinoNunchuk::analogX = values[0];
  ArduinoNunchuk::analogY = values[0];

values[0] can't contain the X and Y values.

Thank you for you time Paul!

Yes, I was thinking that too but I attributed those values like as a test, it was the only way to make all the axis and buttons show up and behave properly (apart from the analogXX).

Maybe you need to print() the values in the values array. See what changes as you move ONE control at a time.