wii motion and nunchuk :~

buenas,hise un poco la tarea, pero aun estoy algo desconcertado con la sinbologia que usa el autor.
he modificado el codigo del MultiWiiCopter, para que solo me tire los datos del
los sensores, lo que no he podido que me tire el estado de los botones del wii nunchuk(siempre me tira cero), debo estar
apuntando hacia otra direccion, alguna idea ?

#define LEDPIN 13
#define POWERPIN 12



// alias for RC
#define ROLL 0
#define PITCH 1
#define YAW 2
#define THROTTLE 3
#define AUX1 4




// **************
// Wii Motion Plus I2C & gyro+nunchuk functions
// **************


static int16_t gyroADC[3];
static int16_t accADC[3];
static uint8_t rawADC[6];

static int16_t angle[2]; //absolute angle inclination in Deg

// Mask prescaler bits : only 5 bits of TWSR defines the status of each I2C request
#define TW_STATUS_MASK	(_BV(TWS7)|_BV(TWS6)|_BV(TWS5)|_BV(TWS4)|_BV(TWS3))
#define TW_STATUS       (TWSR & TW_STATUS_MASK)

void i2c_init(void) {
  PORTC |= 1<<4; // activate internal pull-ups PIN A4 for twi
  PORTC |= 1<<5; // activate internal pull-ups PIN A5 for twi
  TWSR = 0;        // no prescaler => prescaler = 1
  TWBR = ((16000000L / 400000L) - 16) / 2; // change the I2C clock rate
  TWCR = 1<<TWEN;  // enable twi module, no interrupt
}

void i2c_rep_start(uint8_t address) {
  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN) | (1<<TWSTO); // send REAPEAT START condition
  waitTransmissionI2C(); // wait until transmission completed
  checkStatusI2C(); // check value of TWI Status Register
  TWDR = address; // send device address
  TWCR = (1<<TWINT) | (1<<TWEN);
  waitTransmissionI2C(); // wail until transmission completed
  checkStatusI2C(); // check value of TWI Status Register
}

void i2c_write(uint8_t data ) {	
  TWDR = data; // send data to the previously addressed device
  TWCR = (1<<TWINT) | (1<<TWEN);
  waitTransmissionI2C(); // wait until transmission completed
  checkStatusI2C(); // check value of TWI Status Register
}

uint8_t i2c_readAck() {
  TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
  waitTransmissionI2C();
  return TWDR;
}

uint8_t i2c_readNak(void) {
  TWCR = (1<<TWINT) | (1<<TWEN);
  waitTransmissionI2C();
  return TWDR;
}

void waitTransmissionI2C() {
  uint8_t count = 255;
  while (count-->0 && !(TWCR & (1<<TWINT)) );
  if (count<2) { //we are in a blocking state => we don't insist
    TWCR = 0;  //and we force a reset on TWINT register
    
  }
}

void checkStatusI2C() {
  if ( (TW_STATUS & 0xF8) == 0xF8) { //TW_NO_INFO : this I2C error status indicates a wrong I2C communication.
    // WMP does not respond anymore => we do a hard reset. I did not find another way to solve it. It takes only 13ms to reset and init to WMP or WMP+NK
    TWCR = 0;
    digitalWrite(POWERPIN,0);
    delay(1);  
    digitalWrite(POWERPIN,1);
    delay(10);  
    i2c_rep_start(0xA6);
    i2c_write(0xF0);
    i2c_write(0x55);
    i2c_rep_start(0xA6);
    i2c_write(0xFE);
    i2c_write(0x05);
    
  }
}

void initI2C(void) {
  i2c_init();
  delay(250);
  i2c_rep_start(0xA6 + 0);//write direction => 0
  i2c_write(0xF0); 
  i2c_write(0x55); 
  delay(250);
  i2c_rep_start(0xA6 + 0);//write direction => 0
  i2c_write(0xFE); 
  i2c_write(0x05); 
  delay(250);
}

void getI2C() {
  i2c_rep_start(0xA4 + 0);//write direction => 0
  i2c_write(0x00);
  i2c_rep_start(0xA4 + 1);//read direction => 1
  for(uint8_t i = 0; i < 5; i++) {
    rawADC[i]=i2c_readAck();}
  rawADC[5]= i2c_readNak();
}

uint8_t rawIMU () {
  getI2C();
  if ( rawADC[5]&0x02 ) {// motion plus data
    gyroADC[PITCH]  = - (((rawADC[4]>>2)<<8) + rawADC[1]);
    gyroADC[ROLL]   = - (((rawADC[5]>>2)<<8) + rawADC[2]);
    gyroADC[YAW]    = - (((rawADC[3]>>2)<<8) + rawADC[0])>>3;
    
    Serial.print( gyroADC[PITCH]);
     Serial.print("  ");
     Serial.print(gyroADC[ROLL]);
     Serial.print("  ");
      Serial.print(gyroADC[YAW] );
      Serial.print("  ");
    return 1;
  } else { //nunchuk data
    accADC[PITCH] = (rawADC[2]<<2) - ((rawADC[5]>>3)&0x2);   // -
    accADC[ROLL]  =  (rawADC[3]<<2) + ((rawADC[5]>>4)&0x2);
    accADC[YAW]   = ((rawADC[4]&0xFE)<<2) - ((rawADC[5]>>5)&0x6); // -
    Serial.print(  rawADC[0],DEC);
    Serial.print("  ");
    Serial.print(  rawADC[1],DEC);
    Serial.print("  ");
     Serial.print( accADC[PITCH] );
     Serial.print("  ");
      Serial.print( accADC[ROLL]);
      Serial.print("  ");
       Serial.print(accADC[YAW]);
       Serial.print("  ");
      Serial.print((rawADC[5] >> 1) & 1 );
      Serial.print("  ");
      Serial.println((rawADC[5] & 1));
    return 0;
  }
}

void setup() {
 Serial.begin (115200);

 initI2C();
 rawIMU ();

}

void loop() {
  
  
  rawIMU ();
  delay(20);
  
}

me refiero en esta parte:

} else { //nunchuk data
accADC[PITCH] = (rawADC[2]<<2) - ((rawADC[5]>>3)&0x2); // -
accADC[ROLL] = (rawADC[3]<<2) + ((rawADC[5]>>4)&0x2);
accADC[YAW] = ((rawADC[4]&0xFE)<<2) - ((rawADC[5]>>5)&0x6); // -
Serial.print( rawADC[0],DEC);
Serial.print(" ");
Serial.print( rawADC[1],DEC);
Serial.print(" ");
Serial.print( accADC[PITCH] );
Serial.print(" ");
Serial.print( accADC[ROLL]);
Serial.print(" ");
Serial.print(accADC[YAW]);
Serial.print(" ");
Serial.print((rawADC[5] >> 1) & 1 );
Serial.print(" ");
Serial.println((rawADC[5] & 1));
return 0;
}

en cuanto dice ionhs: que mala onda y ensima apoya a la censura, aveces una cara puede decir que mil palabras. asi que le pido a Sr.David si me podria
dar Admin, asi lo baneo a ionhs, "me cae mal". ]:smiley: