Nunchuck Mouse Arduino 0022

I’ve been trying to replicate the Nunchuck Mouse project done by Frank Zhao (http://www.uchobby.com/index.php/2008/04/19/wii-nunchuck-mouse-code-breakdown/).

It’s been stated that the code does indeed still work with the newer versions of Arduino, except some code needs to be moved around. After inserting a header file here and there, I’ve been able to make it compile properly, along with the AutoIt code that is required as well. The problem is it just isn’t doing anything when hooked up to the computer etc. I’m getting no errors.

Can anyone please help point me in the right direction to make the code work? I’d appreciate any insight.

Here’s what I have compiling, both the Arduino code and the AutoIt code:
The AutoIt code is identical to what is included in the above link

This is the code for the Arduino Uno 0022

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) // clear bit
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) // set bit
#include <stdlib.h>
#include <avr/interrupt.h>  
#include <avr/io.h>
#include "C:\Documents and Settings\user\My Documents\Arduino\i2c.h" // change file path

[code]#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) // clear bit
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) // set bit
#include <stdlib.h>
#include <avr/interrupt.h>  
#include <avr/io.h>
#include "C:\Documents and Settings\user\My Documents\Arduino\i2c.h" // change file path


// variables
int joyX;
int joyY;
int accelX;
int accelY;
int accelZ;
int xAngle;
int yAngle;
byte _joyX;
byte _joyY;
byte _accelX;
byte _accelY;
byte _accelZ;
byte butC;
byte butZ;
byte buttons;

void nunchuckInit()
{
  twi_init(); // initialize i2c

  delay(10); // wait a bit

  // send init to nunchuck
  byte data[2];
  data[0] = 0x40;
  data[1] = 0x00;
  twi_writeTo(0x52, data, 2, 1);
}

int chuckProc(byte _data, int min, int max)
{
  long data = _data;

  // make sure within range
  if(data < min)
  {
    data = min;
  }
  else if(data > max)
  {
    data = max;
  }

  // normalize values
  data -= min;
  data = (255 * ((data * 100) / (max - min))) / 100;
  data -= 127;

  // make sure within range again
  if(data < -127)
  {
    data = -127;
  }
  else if(data > 127)
  {
    data = 127;
  }

  if(data < 15 && data > -15)
  {
    data = 0;
  }

  int data_ = data;

  return data_;
}

void nunchuckRead()
{
  // request data
  byte data[6];	
  twi_readFrom(0x52, data, 6);

  // read data from buffer
  byte i;
  for(i = 0; i < 6; i++)
  {
    data[i] = twi_masterBuffer[i];
  }
  
  // decipher data
  _joyX = (data[0] ^ 0x17) + 0x17;
  _joyY = (data[1] ^ 0x17) + 0x17;
  _accelX = (data[2] ^ 0x17) + 0x17;
  _accelY = (data[3] ^ 0x17) + 0x17;
  _accelZ = (data[4] ^ 0x17) + 0x17;
  buttons = (data[5] ^ 0x17) + 0x17;

  // get ready for next request
  data[0] = 0x00;
  twi_writeTo(0x52, data, 1, 1);

  // process raw data into good usuable ranges and angles
  joyX = chuckProc(_joyX, 33, 223);
  joyY = chuckProc(_joyY, 34, 223);
  accelX = chuckProc(_accelX, 0x48, 0xB0);
  //xAngle = accelToAngle(accelX);
  accelY = chuckProc(_accelY, 0x46, 0xAF);
  //yAngle = accelToAngle(accelY);
  accelZ = chuckProc(_accelZ, 0x4A, 0xB1);
  buttons &= B00000011;

  // find buttons
  if(bit_is_set(buttons, 1))
  {
    butC = 0;
  }
  else
  {
    butC = 1;
  }

  if(bit_is_set(buttons, 0))
  {
    butZ = 0;
  }
  else
  {
    butZ = 1;
  }
}

void setup()
{
  Serial.begin(115200); // initialize serial port
  nunchuckInit(); // initialize nunchuck
}

void loop()
{
  nunchuckRead(); // read data from nunchuck

  Serial.print(255, BYTE); // sync byte to computer

  if(butZ == 1)
  {
    Serial.print("l"); // left click
  }
  else
  {
    Serial.print(" ");
  }

  if(butC == 1)
  {
    Serial.print("r"); // right click
  }
  else
  {
    Serial.print(" ");
  }

  if(yAngle > 45 || yAngle < 0 - 45)
  {
    // if tilted

    if(joyY < 0 - 100)
    {
      Serial.print("d"); // scroll down
    }
    else if(joyY > 100)
    {
      Serial.print("u"); // scroll up
    }
    else
    {
      if(joyX > 100)
      {
        Serial.print("m"); // middle click
      }
      else if(joyX < 0 - 100)
      {
        Serial.print("b"); // back button
      }
      else
      {
        Serial.print(" ");
      }
    }

    joyX = 0; // if tilted, don't move the cursor
    joyY = 0;
  }
  else
  {
    Serial.print(" ");
  }

  Serial.print((joyX / 2) + (127 / 2), BYTE); // send joystick data
  Serial.print((joyY / 2) + (127 / 2), BYTE);

  Serial.println();
  delay(50); // delay so no overflow on computer
}

I should also mention that for the time being I’m commenting out the accelerometer code. If anyone has any familiarity with this project, or has any insight, I would greatly appreciate any help or ideas.

Thanks.[/code]

What is being printed in arduino’s serial monitor?

Hmm interestingly enough, I'm not getting anything...I have it set to the correct baud rate and everything. Are you familiar with this project at all?

HOLY CRAP THAT PAGE IS OLD That's my work you linked to... I was still in high school when I wrote that... It's pretty bad lol The updated project is here: http://frank.circleofcurrent.com/index.php?page=nunchuk_mouse_joystick

err... just realized I didn't write much, or anything for that... Do you want the source code for it? It uses USB HID instead of AutoIt

oh and TWI code might freeze if something is funky on the bus

oh and I'm not sure but make sure that AutoIt isn't resetting your UNO since this was made in 2008 when auto-reset didn't exist yet

HAHA! Well this is awesome news, the man himself! I would really appreciate the source code if possible. I was looking for a neat project using the Wii Nunchuck. I'll look into the USB HID. The idea of AutoIt seemed really cool but honestly, I don't have experience with Basic so who knows what the issue I'm having is.

But if you don't mind, I'd really appreciate the source code for the newer version. Is it safe to assume the same basics apply in terms of your description for the code on the uchobby site?

Thanks for the response

New V-USB based code http://code.google.com/p/circle-of-current/downloads/detail?name=usb_nunchuk_code.zip It runs at 12 MHz, targeting the USnooBie (see my signature), if you want to use it with 16 MHz Arduino, reconfiguration and recompiling is required

Thanks for the update, Frank. I’m looking forward to trying this out, it’s a very interesting project.