Pages: [1]   Go Down
Author Topic: nunchuck library ???  (Read 492 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i can not get any of the libraries that get data from the Wi nunchuck to work on the DUE. does anyone have a library that works on the DUE or some suggestions as to how to use I2C to communicate with it?
Logged

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

found the basic wire code needed and can read the nunchuck but it is horribly slooooow and also gets garbage every second read. does anyone have experience with this and know if this is "normal" i have tried to find documentation on what the nunchuck wants for initialization and have found various suggestions but nothing works any better. here is s simple sketch that does not use a library. all the nunchuck functionality is in the sketch with wire function. i am connecting clock and data lines to pins 21 and 20 and using 3.3 volt power, not analog pin power.

Code:
/*
 * NunchuckPrint
 *
 * 2007 Tod E. Kurt, http://todbot.com/blog/
 *
 * The Wii Nunchuck reading code is taken from Windmeadow Labs
 *   http://www.windmeadow.com/node/42
 */
 
#include <Wire.h>

void setup()
{
  Serial.begin(9600);
//  nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
  nunchuck_init(); // send the initilization handshake
  Serial.print ("Finished setup\n");
}

void loop()
{
  if (nunchuck_get_data()) nunchuck_print_data();
//  nunchuck_print_data();
  delay(100);
}
//
// Nunchuck functions
//

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

// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
//void nunchuck_init()
//{
//  Wire.begin();                   // join i2c bus as master
//  Wire.beginTransmission(0x52);   // transmit to device 0x52
//  Wire.write(0x40);      // sends memory address
//  Wire.write(0x00);      // sends sent a zero. 
//  Wire.endTransmission();   // stop transmitting
//}
void nunchuck_init() // another suggestion for init
{
  Wire.begin();                   // join i2c bus as master
  Wire.beginTransmission(0x52);   // transmit to device 0x52
  Wire.write(0xF0);      // sends memory address
  Wire.write(0X55);      // sends 55 
  Wire.write(0xFB);      // sends memory address
  Wire.write(0x00);      // sends 0   
  Wire.endTransmission();   // stop transmitting
}

// Send a request for data to the nunchuck
// was "send_zero()"
void nunchuck_send_request()
{
  Wire.beginTransmission(0x52);   // transmit to device 0x52
  Wire.write(0x00);      // sends one byte
  Wire.endTransmission();   // stop transmitting
}

// Receive data back from the nunchuck,
int nunchuck_get_data()
{
    int cnt=0;
//    nunchuck_send_request();  // send request for next data payload
    Wire.requestFrom (0x52, 6);   // request data from nunchuck
    while (Wire.available ()) {
      // receive byte as an integer
//      nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());
      nunchuck_buf[cnt] = Wire.read();
      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
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 += 2;
  if ((nunchuck_buf[5] >> 3) & 1)
    accel_x_axis += 1;

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

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

  Serial.print(i,DEC);
  Serial.print("\t");
 
//  Serial.print("joy:");
//  Serial.print(joy_x_axis,DEC);
  Serial.print("Y ");
  Serial.println(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++;
}

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

Pages: [1]   Go Up
Jump to: