Una vez descargado añade la carpeta completa a tu carpeta “Library” en Arduino. Desde ese momento podrás usar tu Wii Nunchuck.
#include <Wire.h>
#include "ArduinoNunchuk.h"
ArduinoNunchuk nunchuk = ArduinoNunchuk();
int xjoystick;
int yjoystick;
int xtilt;
int ytilt;
void setup() {
Serial.begin(9600);
nunchuk.init();
}
void loop(){
xjoystick = nunchuk.analogX;
xjoystick = constrain(xjoystick, 26, 226);
xjoystick = map(xjoystick, 26, 226, 0, 255);
analogWrite(9, xjoystick);
yjoystick = nunchuk.analogY;
yjoystick = constrain(yjoystick, 26, 226);
yjoystick = map(yjoystick, 26, 226, 0, 255);
analogWrite(6, yjoystick);
xtilt = nunchuk.accelX;
xtilt = constrain(xtilt, 320, 720);
xtilt = map(xtilt, 320, 720, 0, 255);
analogWrite(5, xtilt);
ytilt = nunchuk.accelY;
ytilt = constrain(ytilt, 320, 720);
ytilt = map(ytilt, 320, 720, 0, 255);
analogWrite(3, ytilt);
Serial.print ("X: ");
Serial.print (xjoystick, DEC);
Serial.print ("\t");
Serial.print ("Y: ");
Serial.print (yjoystick, DEC);
Serial.print ("\t");
Serial.print ("X: ");
Serial.print (xtilt, DEC);
Serial.print ("\t");
Serial.print ("Y: ");
Serial.print (ytilt, DEC);
Serial.print ("\t");
nunchuk.update();
if(nunchuk.cButton == 1 && nunchuk.zButton != 1){
digitalWrite(10, HIGH);
Serial.print("--C-- ");
}
else{
digitalWrite(10, LOW);
}
if(nunchuk.zButton == 1 && nunchuk.cButton != 1){
digitalWrite(11, HIGH);
Serial.print("--Z-- ");
}
else{
digitalWrite(11, LOW);
}
if(nunchuk.cButton == 1 && nunchuk.zButton == 1){
digitalWrite(10, HIGH);
Serial.print("--Z-C--");
}
else{
digitalWrite(10, LOW);
}
Serial.print ("\r\n");
}
Puedes descargar el código desde: http://www.mediafire.com/?ypcu300zkuaho8w
Fuente: https://multitechnologyblog.wordpress.com/
No hay comentarios:
Publicar un comentario