Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #define nunchuk_ID 0xA4 >> 1
- unsigned char buffer[6];
- int cnt = 0;
- void setup ()
- {
- Serial.begin (9600);
- Wire.begin ();
- nunchuck_init ();
- delay (100);
- }
- void nunchuck_init ()
- {
- Wire.beginTransmission (nunchuk_ID);
- Wire.write (0x40);
- Wire.write (0x00);
- Wire.endTransmission ();
- }
- void send_zero ()
- {
- Wire.beginTransmission (nunchuk_ID);
- Wire.write (0x00);
- Wire.endTransmission ();
- }
- void loop ()
- {
- Wire.requestFrom (nunchuk_ID, 6);
- while (Wire.available ())
- {
- buffer[cnt] = nunchuk_decode_byte (Wire.read ());
- cnt++;
- }
- if (cnt >= 5)
- {
- print ();
- }
- cnt = 0;
- send_zero ();
- delay (100);
- }
- void print ()
- {
- unsigned char joy_x_axis;
- unsigned char joy_y_axis;
- int accel_x_axis;
- int accel_y_axis;
- int accel_z_axis;
- unsigned char z_button;
- unsigned char c_button;
- joy_x_axis = buffer[0];
- joy_y_axis = buffer[1];
- accel_x_axis = (buffer[2]) << 2;
- accel_y_axis = (buffer[3]) << 2;
- accel_z_axis = (buffer[4]) << 2;
- if ((buffer[5] & 0x01)!=0)
- { z_button = 1; }
- else
- { z_button = 0; }
- if ((buffer[5] & 0x02)!=0)
- { c_button = 1; }
- else
- { c_button = 0; }
- accel_x_axis += ((buffer[5]) >> 2) & 0x03;
- accel_y_axis += ((buffer[5]) >> 4) & 0x03;
- accel_z_axis += ((buffer[5]) >> 6) & 0x03;
- Serial.print (joy_x_axis, DEC);
- Serial.print ("\t");
- Serial.print (joy_y_axis, DEC);
- Serial.print ("\t");
- Serial.print (accel_x_axis, DEC);
- Serial.print ("\t");
- Serial.print (accel_y_axis, DEC);
- Serial.print ("\t");
- Serial.print (accel_z_axis, DEC);
- Serial.print ("\t");
- Serial.print (z_button, DEC);
- Serial.print ("\t");
- Serial.print (c_button, DEC);
- Serial.print ("\r\n");
- }
- char nunchuk_decode_byte (char x)
- {
- x = (x ^ 0x17) + 0x17;
- return x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement