Skip to content

Instantly share code, notes, and snippets.

@creationix
Created February 10, 2015 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/1136e40c06490cb79bce to your computer and use it in GitHub Desktop.
Save creationix/1136e40c06490cb79bce to your computer and use it in GitHub Desktop.
/*
* ArduinoNunchuk.h - Improved Wii Nunchuk library for Arduino
*
* Copyright 2011-2013 Gabriel Bianconi, http://www.gabrielbianconi.com/
*
* Project URL: http://www.gabrielbianconi.com/projects/arduinonunchuk/
*
* Based on the following resources:
* http://www.windmeadow.com/node/42
* http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
* http://wiibrew.org/wiki/Wiimote/Extension_Controllers
*
*/
#ifndef ArduinoNunchuk_H
#define ArduinoNunchuk_H
class ArduinoNunchuk
{
public:
int analogX;
int analogY;
int accelX;
int accelY;
int accelZ;
int zButton;
int cButton;
void init();
void update();
private:
void _sendByte(byte data, byte location);
};
#endif
/*
* ArduinoNunchuk.cpp - Improved Wii Nunchuk library for Arduino
*
* Copyright 2011-2013 Gabriel Bianconi, http://www.gabrielbianconi.com/
*
* Project URL: http://www.gabrielbianconi.com/projects/arduinonunchuk/
*
* Based on the following resources:
* http://www.windmeadow.com/node/42
* http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
* http://wiibrew.org/wiki/Wiimote/Extension_Controllers
*
*/
#define ADDRESS 0x52
void ArduinoNunchuk::init()
{
Wire.begin();
ArduinoNunchuk::_sendByte(0x55, 0xF0);
ArduinoNunchuk::_sendByte(0x00, 0xFB);
ArduinoNunchuk::update();
}
void ArduinoNunchuk::update()
{
int count = 0;
int values[6];
Wire.requestFrom(ADDRESS, 6);
while(Wire.available())
{
values[count] = Wire.read();
count++;
}
ArduinoNunchuk::analogX = values[0];
ArduinoNunchuk::analogY = values[1];
ArduinoNunchuk::accelX = (values[2] << 2) | ((values[5] >> 2) & 3);
ArduinoNunchuk::accelY = (values[3] << 2) | ((values[5] >> 4) & 3);
ArduinoNunchuk::accelZ = (values[4] << 2) | ((values[5] >> 6) & 3);
ArduinoNunchuk::zButton = !((values[5] >> 0) & 1);
ArduinoNunchuk::cButton = !((values[5] >> 1) & 1);
ArduinoNunchuk::_sendByte(0x00, 0x00);
}
void ArduinoNunchuk::_sendByte(byte data, byte location)
{
Wire.beginTransmission(ADDRESS);
Wire.write(location);
Wire.write(data);
Wire.endTransmission();
delay(10);
}
#define BAUDRATE 19200
ArduinoNunchuk nunchuk = ArduinoNunchuk();
void setup()
{
Serial.begin(BAUDRATE);
delay(400);
nunchuk.init();
}
void loop()
{
nunchuk.update();
Serial.print(nunchuk.analogX, DEC);
Serial.print(' ');
Serial.print(nunchuk.analogY, DEC);
Serial.print(' ');
Serial.print(nunchuk.accelX, DEC);
Serial.print(' ');
Serial.print(nunchuk.accelY, DEC);
Serial.print(' ');
Serial.print(nunchuk.accelZ, DEC);
Serial.print(' ');
Serial.print(nunchuk.zButton, DEC);
Serial.print(' ');
Serial.println(nunchuk.cButton, DEC);
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment