WiiChuck

This is some basic information on how to interface with a Wii Nunchuck Controller.

Hardware

Library

Import Library:

http://mbed.org/projects/cookbook/svn/WiiChuck/trunk

Import Example program:

http://mbed.org/projects/cookbook/svn/WiiChuck/examples/WiiChuck

WiiTest

#include "mbed.h"
#include "WiiChuck.h"

WiiChuck wii(p28, p27);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx

int main() {
    wii.init();
    while(1){
        pc.printf("Z: %x\r\n",wii.buttonz());
        pc.printf("C: %x\r\n",wii.buttonc());
        pc.printf("Accel X: %1.4f\r\n",wii.axisx());
        pc.printf("Accel Y: %1.4f\r\n",wii.axisy());
        pc.printf("Accel Z: %1.4f\r\n",wii.axisz());
        pc.printf("Joystick X: %1.4f\r\n",wii.joyx());
        pc.printf("Joystick Y: %1.4f\r\n",wii.joyy());
        pc.printf("\n");
        wait(0.5);
    }
}
WiiTest.bin
Source Code Binary

Resources

Datasheets


Development Log

Finally got the output going after fighting with the code for a while.

Next steps will be to create a library for it.

Also, while all documentation I'm reading has the z and c buttons as being 0 for pressed and 1 for released, the code above shows the following:

z c
When both pressed 0 1
With just z pressed 0 0
With just c pressed 1 0
Both open 1 1

The last 2 are as expected, something weird is going on with the first 2 though. I need to look at the code a bit closer.

Also need to see about some way to calibrate the accelerometers.


Ok, got the library done. The values still need a bit of calibration. I'll try to add that into the init function as soon as I get some more time. I also fixed the issue with the Z and C buttons. The function now reports each correctly, and with a 1 when the button is pressed. So to contrast with the table above, the new values are:

z c
When both pressed 1 1
With just z pressed 1 0
With just c pressed 0 1
Both open 0 0

I think that makes a bit more sense.


Doing some more testing. I've come across a timing issue when trying to do multiple reads against different methods quickly. For example: int val1 = wii.buttonz(); int val2 = wii.buttonc(); starts to give out some weird stuff pretty quickly. I think it has to do with the reads coming too quickly together. It works fine when there are printf statements because those are slow enough to give it some breathing room. I've had to incorporate some wait statements into my code to make it behave properly. I'm debating about building this into the library or maybe rewriting it to perhaps fix the root of the problem as much as I can. Always things to do!