buenas, he podido hacer funcionar los dispositivos por separados,
pero juntos no he podido.ahi le dejos los codes.desde ya muchas gracias por su ayuda.
/////Code nunchuk
#include <Wire.h> // Necesario para trabajar con el bus I2C
#include <utility\twi.h> // Necesario para la constante CPU_FREQ
void setup()
{
Serial.begin(9600); // Inicializamos el puerto serie a una velocidad baja para que se puedan ver los datos
Wire.begin(); // Inicializamos la librería wire para trabajar con I2C
TWBR = ((CPU_FREQ / 400000L) - 16) / 2; // Configuramos el I2C para el modo Fast (400Kb/s)
// Inicializamos el nunchuk sin encriptación
Wire.beginTransmission(0x52);
Wire.send(0xF0);
Wire.send(0x55);
Wire.endTransmission();
Wire.beginTransmission(0x52);
Wire.send(0xFB);
Wire.send(0x00);
Wire.endTransmission();
}
void loop ()
{
uint8_t buffer[6];
// Pedimos que se actualicen los datos
Wire.beginTransmission(0x52);
Wire.send(0);
Wire.endTransmission();
// Recogemos y guardamos los datos
Wire.requestFrom(0x52, 6);
for(int indice = 0; indice < 6; indice++)
{
buffer[indice] = Wire.receive();
}
// Imprimimos los datos en el puerto serie
//Serial.print("JX:");
Serial.print(buffer[0], DEC);
Serial.print(",");
Serial.print(buffer[1], DEC);
Serial.print(",");
Serial.print((buffer[2] << 2) | ((buffer[5] & 0x0c) >> 2));
Serial.print(",");
Serial.print((buffer[3] << 2) | ((buffer[5] & 0x30) >> 4));
Serial.print(",");
Serial.print((buffer[4] << 2) | ((buffer[5] & 0xc0) >> 6));
Serial.print(",");
Serial.print((buffer[5] >> 1) & 1);
Serial.print(",");
Serial.println(buffer[5] & 1);
}
/// wii motion
#include <Wire.h>
byte data[6]; //six data bytes
int yaw, pitch, roll; //three axes
int yaw0, pitch0, roll0; //calibration zeroes
void wmpOn(){
Wire.beginTransmission(0x53); //WM+ starts out deactivated at address 0x53
Wire.send(0xfe); //send 0x04 to address 0xFE to activate WM+
Wire.send(0x04);
Wire.endTransmission(); //WM+ jumps to address 0x52 and is now active
}
void wmpSendZero(){
Wire.beginTransmission(0x52); //now at address 0x52
Wire.send(0x00); //send zero to signal we want info
Wire.endTransmission();
}
void calibrateZeroes(){
for (int i=0;i<10;i++){
wmpSendZero();
Wire.requestFrom(0x52,6);
for (int i=0;i<6;i++){
data[i]=Wire.receive();
}
yaw0+=(((data[3]>>2)<<8)+data[0])/10; //average 10 readings
pitch0+=(((data[4]>>2)<<8)+data[1])/10;
roll0+=(((data[5]>>2)<<8)+data[2])/10;
}
Serial.print("Yaw0:");
Serial.print(yaw0);
Serial.print(" Pitch0:");
Serial.print(pitch0);
Serial.print(" Roll0:");
Serial.println(roll0);
}
void receiveData(){
wmpSendZero(); //send zero before each request (same as nunchuck)
Wire.requestFrom(0x52,6); //request the six bytes from the WM+
for (int i=0;i<6;i++){
data[i]=Wire.receive();
}
yaw=((data[3]>>2)<<8)+data[0]-yaw0;
pitch=((data[4]>>2)<<8)+data[1]-pitch0;
roll=((data[5]>>2)<<8)+data[2]-roll0;
}
//see http://wiibrew.org/wiki/Wiimote/Extension_Controllers#Wii_Motion_Plus
//for info on what each byte represents
void setup(){
Serial.begin(115200);
Serial.println("WM+ tester");
Wire.begin();
wmpOn(); //turn WM+ on
calibrateZeroes(); //calibrate zeroes
delay(1000);
}
void loop(){
receiveData(); //receive data and calculate yaw pitch and roll
Serial.print("yaw:");//see diagram on randomhacksofboredom.blogspot.com
Serial.print(yaw); //for info on which axis is which
Serial.print(" pitch:");
Serial.print(pitch);
Serial.print(" roll:");
Serial.println(roll);
delay(100);
}