Go Down

Topic: Brand new Wii Nunchuck library for Arduino 1.0 (Read 13168 times) previous topic - next topic

alexhb

Hey everyone,
While checking out the old "wiichuck demo" code online, I realized that it was based on Arduino 0010, pretty outdated! But I came across a brand new library, released the 20th!
http://www.gabrielbianconi.com/blog/improved-wii-nunchuk-library-for-arduino/
The library was made by Gabriel Bianconi, not me. I tried it out and it works very well from what I've tested so far. It's a great way to set up a Nunchuk quickly, almost "plug and play". Wrote up a quick code to show activity through a row of LED's... haven't got it all commented yet, but it's relatively harmless. :)

Code: [Select]
#include <Wire.h>
#include "ArduinoNunchuk.h"


ArduinoNunchuk nunchuk = ArduinoNunchuk();

int xjoystick;
int yjoystick;
int xtilt;
int ytilt;

void setup() {
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(3, OUTPUT);

  nunchuk.init();
}

void loop(){ 
  nunchuk.update();
  if(nunchuk.cButton == 1 && nunchuk.zButton != 1){
    digitalWrite(12, HIGH);
  }
  else{
    digitalWrite(12, LOW);
  }
  if(nunchuk.zButton == 1 && nunchuk.cButton != 1){
    digitalWrite(11, HIGH);
  }
  else{
    digitalWrite(11, LOW);
  }
  if(nunchuk.cButton == 1 && nunchuk.zButton == 1){
    digitalWrite(10, HIGH);
  }
  else{
    digitalWrite(10, LOW);
  }

  xjoystick = nunchuk.analogX;
  xjoystick = constrain(xjoystick, 26, 226);
  xjoystick = map(xjoystick, 26, 226, 0, 255);
  analogWrite(9, xjoystick);

  yjoystick = nunchuk.analogY;
  yjoystick = constrain(yjoystick, 26, 226);
  yjoystick = map(yjoystick, 26, 226, 0, 255);
  analogWrite(6, yjoystick);

  xtilt = nunchuk.accelX;
  xtilt = constrain(xtilt, 320, 720);
  xtilt = map(xtilt, 320, 720, 0, 255);
  analogWrite(5, xtilt);
 
  ytilt = nunchuk.accelY;
  ytilt = constrain(ytilt, 320, 720);
  ytilt = map(ytilt, 320, 720, 0, 255);
  analogWrite(3, ytilt);
}


Have fun!
~Alex
"#define QUESTION ((bb) || !(bb))  - Shakespeare."


Nyo

I will use this library to connect my nunchuck! Seems really simple and clean, thanks!

Mhn maybe I can shot a question :D

Do you know any software that takes these serial input from the nunchuck(via Arduino) and recognize them as a HID joystick or something similar?
I just want to you is as a joystick for mame/vice emulators...


StrikeEagleIII

I'm trying to get this working and I've hooked the data pin to A4, clock pin to A5 (per the wire library instructions, I have an Uno), pwr pin to arduino 3.3 v and ground to arduino ground, however when I hook any two of the pins up, the serial monitor freezes.  As soon as I disconnect the wires, the serial monitor resumes updating.  I tried resetting the Uno with the serial monitor open and stuff plugged in, but still no luck.  Any ideas?
Thanks,
Jason

StrikeEagleIII

Nevermind--I'm an idiot...  Instead of the wire being plugged into the 3.3V header pin, it was plugged into the reset pin which happens to be right next to it...Works great now.  Thanks for the library.

Go Up