Wii U Nunchuk + Serial Monitor problems

Hello, all!

I've scoured the web for anything related to my help, and checked these forums before signing up, so I hope I've made no mistakes and that I posted in the correct section. I have to make a project involving Arduino and Processing for a class, but I'm having trouble with my Wii U Nunchuk controller and the Serial Monitor.

Disclaimer: I've never heard of Arduino nor have ever touched anything similar to it before a week or two ago. We've been testing, uh, test code with Arduino machines in order to learn how it works. The most coding I know is intermediate HTML/CSS -- so I know nothing about C+++. Sorry. I've had to copy/paste codes from other places.

So I connect everything properly, and upload my sketch. Once done, I check the serial monitor. It does nothing except scroll down four rows of numbers that don't move at all no matter if I use the X- or Y-axis of the Nunchuk controller, waving it like a loon, or pressing the Z or C buttons. Nothing online seems to have a solution and even my own professor (who's used it for a good deal of his life) seems stumped. Below is what I've used, I hope I've posted it correctly:

Here is the code I'm using for the 'demo test', from this blog: “WiiChuck” Wii Nunchuck Adapter Available – todbot blog It's the only code that has worked for me for this project. EDIT: Forgot to say that yes, I even used the Wii Library that's here: Arduino Playground - WiiChuckClass and that didn't work AT ALL. I kept getting "a COM error on Arduino UNO."

/*
 * WiiChuckDemo -- 
 *
 * 2008 Tod E. Kurt, http://thingm.com/
 *
 */

#include <Wire.h>
#include "nunchuck_funcs.h"

int loop_cnt=0;

byte accx,accy,zbut,cbut;
int ledPin = 13;


void setup()
{
    Serial.begin(19200);
    nunchuck_setpowerpins();
    nunchuck_init(); // send the initilization handshake
    
    Serial.print("WiiChuckDemo ready\n");
}

void loop()
{
    if( loop_cnt > 100 ) { // every 100 msecs get new data
        loop_cnt = 0;

        nunchuck_get_data();

        accx  = nunchuck_accelx(); // ranges from approx 70 - 182
        accy  = nunchuck_accely(); // ranges from approx 65 - 173
        zbut = nunchuck_zbutton();
        cbut = nunchuck_cbutton(); 
            
        Serial.print("accx: "); Serial.print((byte)accx,DEC);
        Serial.print("\taccy: "); Serial.print((byte)accy,DEC);
        Serial.print("\tzbut: "); Serial.print((byte)zbut,DEC);
        Serial.print("\tcbut: "); Serial.println((byte)cbut,DEC);
    }
    loop_cnt++;
    delay(1);
}

The function file:

/*
 * Nunchuck functions  -- Talk to a Wii Nunchuck
 *
 * This library is from the Bionic Arduino course : 
 *                          http://todbot.com/blog/bionicarduino/
 *
 * 2007-11 Tod E. Kurt, http://todbot.com/blog/
 *
 * The Wii Nunchuck reading code originally from Windmeadow Labs
 *   http://www.windmeadow.com/node/42
 */

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
//#define Wire.write(x) Wire.send(x)
//#define Wire.read() Wire.receive()
#endif



static uint8_t nunchuck_buf[6];   // array to store nunchuck data,

// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
    DDRC |= _BV(pwrpin) | _BV(gndpin);
    PORTC &=~ _BV(gndpin);
    PORTC |=  _BV(pwrpin);
    delay(100);  // wait for things to stabilize        
}

// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
static void nunchuck_init()
{ 
    Wire.begin();                // join i2c bus as master
    Wire.beginTransmission(0x52);// transmit to device 0x52
#if (ARDUINO >= 100)
    Wire.write((uint8_t)0x40);// sends memory address
    Wire.write((uint8_t)0x00);// sends sent a zero.  
#else
    Wire.send((uint8_t)0x40);// sends memory address
    Wire.send((uint8_t)0x00);// sends sent a zero.  
#endif
    Wire.endTransmission();// stop transmitting
}

// Send a request for data to the nunchuck
// was "send_zero()"
static void nunchuck_send_request()
{
    Wire.beginTransmission(0x52);// transmit to device 0x52
#if (ARDUINO >= 100)
    Wire.write((uint8_t)0x00);// sends one byte
#else
    Wire.send((uint8_t)0x00);// sends one byte
#endif
    Wire.endTransmission();// stop transmitting
}

// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
static char nunchuk_decode_byte (char x)
{
    x = (x ^ 0x17) + 0x17;
    return x;
}

// Receive data back from the nunchuck, 
// returns 1 on successful read. returns 0 on failure
static int nunchuck_get_data()
{
    int cnt=0;
    Wire.requestFrom (0x52, 6);// request data from nunchuck
    while (Wire.available ()) {
        // receive byte as an integer
#if (ARDUINO >= 100)
        nunchuck_buf[cnt] = nunchuk_decode_byte( Wire.read() );
#else
        nunchuck_buf[cnt] = nunchuk_decode_byte( Wire.receive() );
#endif
        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
}

// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits.  That is why I
// multiply them by 2 * 2
static void nunchuck_print_data()
{ 
    static int i=0;
    int joy_x_axis = nunchuck_buf[0];
    int joy_y_axis = nunchuck_buf[1];
    int accel_x_axis = nunchuck_buf[2]; // * 2 * 2; 
    int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
    int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;

    int z_button = 0;
    int c_button = 0;

    // byte nunchuck_buf[5] contains bits for z and c buttons
    // it also contains the least significant bits for the accelerometer data
    // so we have to check each bit of byte outbuf[5]
    if ((nunchuck_buf[5] >> 0) & 1) 
        z_button = 1;
    if ((nunchuck_buf[5] >> 1) & 1)
        c_button = 1;

    if ((nunchuck_buf[5] >> 2) & 1) 
        accel_x_axis += 1;
    if ((nunchuck_buf[5] >> 3) & 1)
        accel_x_axis += 2;

    if ((nunchuck_buf[5] >> 4) & 1)
        accel_y_axis += 1;
    if ((nunchuck_buf[5] >> 5) & 1)
        accel_y_axis += 2;

    if ((nunchuck_buf[5] >> 6) & 1)
        accel_z_axis += 1;
    if ((nunchuck_buf[5] >> 7) & 1)
        accel_z_axis += 2;

    Serial.print(i,DEC);
    Serial.print("\t");

    Serial.print("joy:");
    Serial.print(joy_x_axis,DEC);
    Serial.print(",");
    Serial.print(joy_y_axis, DEC);
    Serial.print("  \t");

    Serial.print("acc:");
    Serial.print(accel_x_axis, DEC);
    Serial.print(",");
    Serial.print(accel_y_axis, DEC);
    Serial.print(",");
    Serial.print(accel_z_axis, DEC);
    Serial.print("\t");

    Serial.print("but:");
    Serial.print(z_button, DEC);
    Serial.print(",");
    Serial.print(c_button, DEC);

    Serial.print("\r\n");  // newline
    i++;
}

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

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

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

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

// returns value of x-axis accelerometer
static int nunchuck_accelx()
{
    return nunchuck_buf[2];   // FIXME: this leaves out 2-bits of the data
}

// returns value of y-axis accelerometer
static int nunchuck_accely()
{
    return nunchuck_buf[3];   // FIXME: this leaves out 2-bits of the data
}

// returns value of z-axis accelerometer
static int nunchuck_accelz()
{
    return nunchuck_buf[4];   // FIXME: this leaves out 2-bits of the data
}

Screenshots of how everything pops up are attached to this post. Four rows whose numbers don't change on the serial monitor no matter what I do with the controller.

Please help. I am getting exceedingly frustrated! Again, I know next to nothing about C+++ and don't want to keep buying or spending money for technology that I'll never be using again. :\ (And that's not hyperbole.)

How are you connecting the Nunchuk to the Arduino? I have used the adapter from todbot with the code you posted and it always works. The Nunchuk will not work if it is not connected to analog pins 4 and 5.

Is there a difference between a WII U Nunchuk and a WII Nunchuk? The Nunchuk that I use is for the older WII system, not WII U, and I'm not sure if it matters.

My professor and I thought the same, that perhaps the age or type of controller makes the difference. I bought last night what I thought was an older model in terms of age, and it didn't work either. I don't have the Wii or Wii U console, so I didn't know there was a difference...

This is the one I'm trying to use. https://www.amazon.com/Wii-Nunchuk-Nunchuck-Controller-Black/dp/B003YLWJLA/ref=pd_bxgy_147_2?_encoding=UTF8&pd_rd_i=B003YLWJLA&pd_rd_r=Q651D4MR7RWMDK9CS141&pd_rd_w=DAPmW&pd_rd_wg=KA6bC&psc=1&refRID=Q651D4MR7RWMDK9CS141

I'm connecting the Nunchuk to the Arduino with this adapter https://www.amazon.com/Gikfun-Adapter-shield-Module-Arduino/dp/B00RK1VKUQ/ref=sr_1_1?ie=UTF8&qid=1487730661&sr=8-1&keywords=Nunchuck+–+Arduino+Adapter I'm using 2,3,4 and 5 like the blog says.

Hmm, I just copy / pasted the code from your post to make super sure I was using the same as you, tested it with my todbot adapter and it works.

The only thoughts I have is that you plugged into digital pins instead of analog pins or the Nunchuk is plugged in upside down (the notch should be on the bottom), two obvious problems that I doubt you've overlooked.

I posted a photo I took now so you can see the setup. It's currently as you say it should be. When I first got it Tuesday, I did make the mistake of plugging the adapter in the digital pins for like 5 minutes, haha.

I went ahead with both ideas (trying an old controller, seeing if there's more than one version of the Nunchuk) and got the same failed results. :cry: There's only the Wii Nunchuk, no one else, but I bought a generic brand one too in case maybe it'd work. It didn't. I also bought an 'older' official brand Wii Nunchuk and nothing. I'm honestly not even sure what to do now at this point. I even tried doing this setup with an older computer just to see if that would do something but nope.

Thank you for the help you've given me so far, though!

EDIT: File size of photo too big, so resized it.

The forum says the maximum length of characters is 9000, so I divided this post into two or three. I don't mean to triple-post but I had two sets of code to show, so I am sorry! (I also didn't see anything against double-posting in general, though?)

On Thursday I kept tinkering around with other codes and the Nunchuk to see if I could somehow by some accident make it finally work. Then I noticed the option to add libraries, and I found the Wiichuck library -- I didn't have anything to lose and I thought maybe this might help in fixing my problem, so I installed it.

This time now, I get an "exit status, error compiling board on Arduino UNO." Searching through the forums, that appears to be more of a "wait again before trying" error? Looking through the error code, it seems to mostly be something to do with the boolean. The code is in the txt file, due to how long it is.

EDIT: Apparently you can add even txt files here! D'oh! I didn't need to make so many posts :-[

boolean error.txt (12.9 KB)

I decided to experiment with the library inclusion again, this time taking out the "include nunchuks_funcs.h" line in the code. I got another error code which, while it seems much more fixable to me than the boolean error, is a little confusing still. It says that I didn't declare the scope of the nunchuks_setpowerpins, but every time I do, it keeps I didn't. Here's the code for that:

F:\SCHOOL BACKUP\Grad SKOOL S2017\Art Gr 522\Week 5 etc Arduino Processing\Actual Project\wiichuck_adapter-master\firmware\WiichuckDemo4\WiichuckDemo4.ino: In function 'void setup()':

WiichuckDemo4:22: error: 'nunchuck_setpowerpins' was not declared in this scope

     nunchuck_setpowerpins();

                           ^

WiichuckDemo4:23: error: 'nunchuck_init' was not declared in this scope

     nunchuck_init(); // send the initilization handshake

                   ^

F:\SCHOOL BACKUP\Grad SKOOL S2017\Art Gr 522\Week 5 etc Arduino Processing\Actual Project\wiichuck_adapter-master\firmware\WiichuckDemo4\WiichuckDemo4.ino: In function 'void loop()':

WiichuckDemo4:33: error: 'nunchuck_get_data' was not declared in this scope

         nunchuck_get_data();

                           ^

WiichuckDemo4:35: error: 'nunchuck_accelx' was not declared in this scope

         accx  = nunchuck_accelx(); // ranges from approx 70 - 182

                                 ^

WiichuckDemo4:36: error: 'nunchuck_accely' was not declared in this scope

         accy  = nunchuck_accely(); // ranges from approx 65 - 173

                                 ^

WiichuckDemo4:37: error: 'nunchuck_zbutton' was not declared in this scope

         zbut = nunchuck_zbutton();

                                 ^

WiichuckDemo4:38: error: 'nunchuck_cbutton' was not declared in this scope

         cbut = nunchuck_cbutton(); 

                                 ^

I'm not sure if I'm closer or way too far away now from a solution, but that's all I've managed to do. I could try to look and see how to solve the boolean code, though I've no clue how in the world boolean even works, outside of Google search keywords....But am I wrong to presume that the second code is closer to my goal? Maybe?