#include #define WII_NUNCHUK_I2C_ADDRESS 0x52 // adresse I2C du nunchuck void setup() { Serial.begin(9600); Wire.begin(); Wire.beginTransmission (WII_NUNCHUK_I2C_ADDRESS); // Séquence d'initialisation Wire.write(0xF0); Wire.write(0x55); Wire.endTransmission(); Wire.beginTransmission (WII_NUNCHUK_I2C_ADDRESS); // Séquence d'initialisation Wire.write(0xFB); Wire.write(0x00); Wire.endTransmission(); } void init_nvlle_acq() // Initialisation pour nouvelle acquisition { Wire.beginTransmission (WII_NUNCHUK_I2C_ADDRESS); Wire.write (0x00); Wire.endTransmission (); } byte buffer[6]; // Buffer contenant les 6 précieux octets qui nous intéressent byte cnt = 0; // index courant de buffer void traitement_affichage() { byte joy_x_axis = buffer[0]; // joystick axe x (0-255) byte joy_y_axis = buffer[1]; // joystick axe y (0-255) int accel_x_axis = buffer[2] << 2; // décale de 2 les 8 bits de l'accéléromètre axe x afin d'obtenir une valeur sur 10 bits int accel_y_axis = buffer[3] << 2; // décale de 2 les 8 bits de l'accéléromètre axe y afin d'obtenir une valeur sur 10 bits int accel_z_axis = buffer[4] << 2; // décale de 2 les 8 bits de l'accéléromètre axe z afin d'obtenir une valeur sur 10 bits byte z_button = 0; // bouton Z byte c_button = 0; // bouton c if ((buffer[5] >> 0) & 1) z_button = 1; if ((buffer[5] >> 1) & 1) c_button = 1; if ((buffer[5] >> 2) & 1) accel_x_axis += 1; if ((buffer[5] >> 3) & 1) accel_x_axis += 2; if ((buffer[5] >> 4) & 1) accel_y_axis += 1; if ((buffer[5] >> 5) & 1) accel_y_axis += 2; if ((buffer[5] >> 6) & 1) accel_z_axis += 1; if ((buffer[5] >> 7) & 1) accel_z_axis += 2; 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 ("\t"); Serial.println(); } void loop() { Wire.requestFrom (WII_NUNCHUK_I2C_ADDRESS, 6); while (Wire.available ()) { buffer[cnt] = Wire.read(); cnt++; } if (cnt >= 5) traitement_affichage(); cnt = 0; init_nvlle_acq(); delay (100); }