Pages: [1]   Go Down
Author Topic: Brand new Wii Nunchuck library for Arduino 1.0  (Read 12684 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 96
There are two ways to write error-free programs; only the third one works.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley

Code:
#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
Logged

"#define QUESTION ((bb) || !(bb))  - Shakespeare."

São Paulo, Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Alex, thanks for sharing my library! smiley-lol
Logged


0
Offline Offline
Full Member
***
Karma: 0
Posts: 152
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mhn maybe I can shot a question smiley-grin

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...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Worked like a charm!  Thanks!  smiley-grin
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: