#include #include #include uint8_t outbuf[6]; // array to store arduino output char storebuf[200]; //array to store arduino input (transmitted from PC) int cnt = 0; int ledPin = 13; void receiveEvent (int howMany) { while (Wire.available ()) { int c = Wire.receive (); // receive byte as an integer char myinput[10]; sprintf (myinput, "%d", c); // convert integer to character strncat (storebuf, myinput, 10); // copy into the input buffer strncat (storebuf, ",", 1); // add a comma cnt++; // after we read 50 bytes, print what we have so far if (cnt > 50) { print (); } digitalWrite (ledPin, HIGH); // sets the LED on } strncat (storebuf, ":", 1); // add a break after each set of input data } void requestEvent () { // Send some data back to the wiimote to be transmitted back to PC outbuf[0] = nunchuk_encode_byte (125); // joystick X outbuf[1] = nunchuk_encode_byte (126); // joystick Y outbuf[2] = nunchuk_encode_byte (227); // Axis X outbuf[3] = nunchuk_encode_byte (241); // Axis Y outbuf[4] = nunchuk_encode_byte (140); // Axis Z outbuf[5] = nunchuk_encode_byte (1); // Press C button, byte[5] is buttons //C,Z and accelaration data //outbuf[5] = nunchuk_encode_byte(2); // Press Z button //outbuf[5] = nunchuk_encode_byte(0); // Press Z and C button Wire.send (outbuf, 6); // send data packet } void setup () { beginSerial (19200); Serial.print ("Finished setup\n"); Wire.begin (0x52); // join i2c bus with address 0x52 Wire.onReceive (receiveEvent); // register event Wire.onRequest (requestEvent); // register event } // Print the input data we have recieved void print () { int s = 0; Serial.println ("Start data dump"); int a = strlen (storebuf); // Get the lenght of the buffer, then print it out one character at a time while (s < (a - 1)) { Serial.print (storebuf[s]); s++; } cnt = 0; strcpy (storebuf, ""); // empty out the storage buffer Serial.println ("End data dump"); } void loop () { delay (100); } // Encode data to format that most wiimote drivers except // only needed if you use one of the regular wiimote drivers char nunchuk_encode_byte (char x) { x = x - 0x17; x = (x ^ 0x17); return x; }