Bueno, pues sigamos con los post paso a paso.... lo primero, como hacer que Arduino (si no sabeis lo que es, darle al enlace y ahi lo explico todo) lea los datos del nunchako..... id cogiendo apuntes
Bueno, sabiendo que el nunchako tiene 3 acelerometros, un joystick y botones por unos 20€, y teniendo en cuentra que un kit similar nos costaria unos 45€ en otros sitios (no voy a poner los enlaces de donde se pueden comprar, los buscais vosotros) pues eso hace que el nunchako sea una herramienta perfecta para poder hacer tus pequeños proyectillos de robotica.

El nunchako tiene su propio conector, asi que corta el final del mismo, en su interior encontraras 4 cables.
white - tierra
red - 3.3+v
green - data
yellow - clock
Une el blanco con la tierra del Arduino, el rojo a 5 voltios, el verde al pin analogico 4, el amarillo al pin analogico 5. El nunchako en teoria solo soporta 3.3 voltios, asi qeu aunque funcione perfectamente con 5 voltios, tened cuidado porque seguramente acortara la vida del nunchako al meterle voltaje de mas.
Este Arduino en particular tiene un chip Atmel atmega168. El atmega 168 puede leer el protocolo TWI. La libreria "Wire" esta en un directorio del Arduino IDE y contiene un numero de librerias que son usadas para leer el protocolo TWI. Solo hay que hacer unos pequeños cambios para hacer la libreria "Wire" la primaria por defecto. el twi.h header necesita dos cambios . Mira dentro de lib/targets/libraries/Wire/utility. Borra twi.o abre twi.h y pon:
// #define ATMEGA8.
Desde que el nunchako usa "Fast" I2C, necesitamos cambiar la velocidad por defecto:
#define TWI_FREQ 400000L.
Para poder comunicar el nunchako con Arduino, tenemos que mandar un apreton de manos. Primeto manda 2 bytes "0x40,0x00". Despues manda un byte "0x00" cada vez que pidas una respuesta del nunchako. Los datos volveran como un fragmento de información de 6 bytes.
Byte Description Values of sample Nunchuk
1 X-axis value of the analog stick Min(Full Left):0x1E / Medium(Center):0x7E / Max(Full Right):0xE1
2 Y-axis value of the analog stick Min(Full Down):0x1D / Medium(Center):0x7B / Max(Full Right):0xDF
3 X-axis acceleration value Min(at 1G):0x48 / Medium(at 1G):0x7D / Max(at 1G):0xB0
4 Y-axis acceleration value Min(at 1G):0x46 / Medium(at 1G):0x7A / Max(at 1G):0xAF
5 Z-axis acceleration value Min(at 1G):0x4A / Medium(at 1G):0x7E / Max(at 1G):0xB1
6 Button state (Bits 0/1) / acceleration LSB Bit 0: "Z"-Button (0 = pressed, 1 = released) / Bit 1: "C" button (0 = pressed, 1 = released) / Bits 2-3: X acceleration LSB /Bits 4:5/ Y acceleration LSB / Bits 6-7: Z acceleration LSB
Aqui esta un simple programa para poder leer datos desde el nunchako, lo leera desde ahi y luego lo mandara a traves de la conexión de puerto serie del Arduino a tu PC. Desde ahi lo puedes leer con cualguier programa para leer los puertos de serie, como el "minicom".
Arduino no puede leer los datos desde el puerto serie y el TWI a la vez, el autor ha hecho pruebas sobre mandar paquetes de 50 bytes desde el nunchako para imprimirlo directamente en el PC, y le da problemas, supone que es porque se interfieren mutuamente dichas conexiones.
- Código: Seleccionar todo
#include <Wire.h>
#include <string.h>
#undef int
#include <stdio.h>
uint8_t outbuf[6]; // array to store arduino output
int cnt = 0;
int ledPin = 13;
void
setup ()
{
beginSerial (19200);
Serial.print ("Finished setup\n");
Wire.begin (); // join i2c bus with address 0x52
nunchuck_init (); // send the initilization handshake
}
void
nunchuck_init ()
{
Wire.beginTransmission (0x52); // transmit to device 0x52
Wire.send (0x40); // sends memory address
Wire.send (0x00); // sends sent a zero.
Wire.endTransmission (); // stop transmitting
}
void
send_zero ()
{
Wire.beginTransmission (0x52); // transmit to device 0x52
Wire.send (0x00); // sends one byte
Wire.endTransmission (); // stop transmitting
}
void
loop ()
{
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ())
{
outbuf[cnt] = nunchuk_decode_byte (Wire.receive ()); // receive byte as an integer
digitalWrite (ledPin, HIGH); // sets the LED on
cnt++;
}
// If we recieved the 6 bytes, then go print them
if (cnt >= 5)
{
print ();
}
cnt = 0;
send_zero (); // send the request for next bytes
delay (100);
}
// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits. That is why I
// multiply them by 2 * 2
void
print ()
{
int joy_x_axis = outbuf[0];
int joy_y_axis = outbuf[1];
int accel_x_axis = outbuf[2] * 2 * 2;
int accel_y_axis = outbuf[3] * 2 * 2;
int accel_z_axis = outbuf[4] * 2 * 2;
int z_button = 0;
int c_button = 0;
// byte outbuf[5] contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf[5]
if ((outbuf[5] >> 0) & 1)
{
z_button = 1;
}
if ((outbuf[5] >> 1) & 1)
{
c_button = 1;
}
if ((outbuf[5] >> 2) & 1)
{
accel_x_axis += 2;
}
if ((outbuf[5] >> 3) & 1)
{
accel_x_axis += 1;
}
if ((outbuf[5] >> 4) & 1)
{
accel_y_axis += 2;
}
if ((outbuf[5] >> 5) & 1)
{
accel_y_axis += 1;
}
if ((outbuf[5] >> 6) & 1)
{
accel_z_axis += 2;
}
if ((outbuf[5] >> 7) & 1)
{
accel_z_axis += 1;
}
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.print ("\r\n");
}
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char
nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
Luego deberias de añadir estas lineas a twi.h para corregir unos bugs que pueden aparecer y puedas hacerlo correr sobre Arduino 10:
- Código: Seleccionar todo
#define ATMEGA8
#ifndef CPU_FREQ^M
#define CPU_FREQ 16000000L
#endif
#ifndef TWI_FREQ^M
#define TWI_FREQ 100000L
#endif
Ya acabada esta parte, estamos listo para poder emprender nuestro verdadero objetivo, que vere si lo puedo poner hoy o mañana, y es donde le vamos a sacar jugo a esto
Lo siento si algunas partes no estan muy claras, pero es que lo he tenido que ir cogiendo de diferentes paginas, y ademas ninguna en castellano y con muchisimo codigo entremedias, asi qeu dudas no os corteis por favor.
Aunque a los demas os pueda aburrir un poco, seguro que a nuestro admin Enock esto si que le gusta....
la principal fuente: http://www.windmeadown.com
EDITO:
Creo qeu con un video se ve mejor todo....















