Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
millerman4487
Published © CC BY-NC-SA

Arduino: Wii Nunchuck Full Functionality

This combines a two-axis joystick, a three-axis accelerometer, and two buttons into one package.

IntermediateProtip30 minutes11,813
Arduino: Wii Nunchuck Full Functionality

Things used in this project

Story

Read more

Schematics

Schematic

Code

Code after library installaton

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

int loop_cnt = 0;

byte joyx, joyy, zbut, cbut, accx, accy, accz;

void _print() {
  Serial.print("Z Button:  ");
  Serial.print(zbut);
  Serial.print("\tC Button:  ");
  Serial.print(cbut);
  Serial.print("\tX Joy:  ");
  Serial.print(map(joyx, 15, 221, 0, 255));
  Serial.print("\tY Joy:  ");
  Serial.print(map(joyy, 29, 229, 0, 255));
  Serial.print("\tX Accel:  ");
  Serial.print(accx);
  Serial.print("\tY Accel:  ");
  Serial.print(accy);
  Serial.println("\tZ Accel:  ");
}

void setup() {
  Serial.begin(9600);
  nunchuck_setpowerpins();
  nunchuck_init(); // send the initilization handshake
  Serial.println("Wii Nunchuck Ready");
}

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

    nunchuck_get_data();

    zbut = nunchuck_zbutton();  //  0 - 1
    cbut = nunchuck_cbutton();  //  0 - 1
    joyx = nunchuck_joyx();     //  15 - 221
    joyy = nunchuck_joyy();     //  29 - 229
    accx = nunchuck_accelx();   //  70 - 182
    accy = nunchuck_accely();   //  65 - 173
    accz = nunchuck_accelz();   //  0 - 255

    _print();
  }
  loop_cnt++;
  delay(1);

}

Credits

millerman4487
10 projects • 82 followers
Thanks to Tod Kurt.

Comments