I've been trying to replicate the Nunchuck Mouse project done by Frank Zhao (Wii Nunchuck Mouse Code Breakdown – uCHobby).
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]