Wii Nunchuck Joystick & Arduino
ABSTRACT
In this project I’ve connected the Arduino board to a touch panel and
then I represent the points where the touch panel is pressed in the screen of
my laptop using a Processing program that reads the data of the serial port.
Figure 1: Arduino connected to the touch panel. |
MATERIALS
1 x Arduino Uno and USB wire
1 x Wii Nunchuck
1 x Wii Nunchuck adapter
DEVELOPMENT
The assembly is very simple with the WiiChuck Adapter. This is a small
PCB designed to be inserted into the connector of a Nintendo Wii Nunchuck to provide access to all 4 wires of the remote
control. The WiiChuck allows you to interface with the Nunchuck without making
a mess and cutting the wires. It use the
I2C (Inter Integrated Circuit) for communicating with the little stick has four
pins called: -, +, c, d. They are connected to GND, +5V, SDA and SCK
respectively. For more information about the I2C and how use the Wii Nunchuck
with Arduino you can read the book of the reference [1] or see [2]. Simplest
way for starting is downloading a Wiichuck library for Arduino. In [3],
everything is explained very well.
Figure 3: Snapshot of Processing program. |
RESULTS
The results
are shown in the next video.
REFERENCE
[1] Programming Interactivity by Joshua Noble. Published
by O’Reilly.
PROPOSALS OF IMPROVEMENT
Create Arduino and Processing codes for using the
buttons and the accelerometer of the Wii Nunchuck for having total control over
the device.
APPENDIX 1
Processing Code:
//Library for read the serial data sent from
Arduino to the Laptop
import processing.serial.*;
Serial myPort;
int baudRate = 9600;
int lf = 10;//SPACE
//Variables globales
int Lados=8;
int led;
float Radio=150,TamanoLed=26;
color
Rojo=color(255,0,0);
color
Limon=color(255,255,100);
void setup()
{
size(400, 400); //Tamaño del programa
//En esta parte se configura el objeto que
lee el puerto serie
println(Serial.list());// List all the available
serial ports: 5
delay(1000);
myPort =
new Serial(this, Serial.list()[3], baudRate); //In my case, COM9 is the Arduino
board and it is in the fourth place.
myPort.bufferUntil(lf);
background(150);//Color de fondo
//En esta parte se dibuja el hexágono
noFill();//Los poligonos no tienen color
stroke(0);//Color de la línea
strokeWeight(3);//Grosor de la línea
poligono(Lados,width/2,height/2,Radio);//Dibuja el hexágono
//En esta parte se dibujan las bombillas leds
strokeWeight(2);//Grosor de la línea
}
void draw()
{
print(led);
Leds(Lados,width/2,height/2,Radio-2,TamanoLed);//Le resto 2 al radio
para que dibuje el círculo más centrado
}
/*Esta función
dibuja un poligono de lado "n", radio "r".
**Cx y Cy son
las coordenadas iniciales donde se comenzará a dibujar el polígono
*/
void poligono(int n, float cx, float cy, float
r)
{
float
angle = 360.0 / n;
beginShape();
for (int
i = 0; i < n; i++)
{
vertex(cx + r * sin(radians(angle * i)), cy + r * cos(radians(angle *
i)));
}
endShape(CLOSE);
}
/*Esta función
dibuja los "n"a una distancia entre ellos de "r"
**Esta basado en
la función del pológono.
*/
void Leds(int n,float cx,float cy,float r, float
dim)
{
int
offset=-90;//I've to put an offset of 45º I want that the number one will be on
top center
float
angle = (360/ n);//Eight sides is equal to angles of 45º
if(led==0) fill(Limon);
else
fill(Rojo);
ellipse(width/2,height/2,TamanoLed,TamanoLed); //En esta parte dibujo un punto negro en el
medio
for (int i = 1; i <= n; i++)
{
if(i==led) fill(Limon);
else fill(Rojo);
ellipse(cx + r * sin(radians(angle * i-45)), cy + r *
(-cos(radians(angle * i-45))),dim,dim);
}
}
//Interruption of the serial port
void serialEvent(Serial p)
{
String
inString;
try
{
inString = (myPort.readString());
led
=int(inString.substring(0,inString.length() - 2));
}catch(Exception e)
{
println(e);
}
redraw();
}
APPENDIX 2
Arduino Code:
/*Code for a Wii Nunchuck connected to Arduino*/
#include //I2C(Inter-Integrated
Circuit) Library: Pin4-->Clock, Pin5-->Data
#include "nunchuck_funcs.h"
int pos=0;
//variable to store the servo posotion
byte joyx,joyy;// set the current coordinates as
the center points
void setup()
{
Serial.begin(9600);
nunchuck_setpowerpins();
nunchuck_init(); //Initialize the I2C system, join the I2C bus and tell
the nunchuck we're talking to it
delay(50);
}
void loop()
{
signed
short int joystickX,joystickY;// The joystick variables
nunchuck_get_data();
joyx=nunchuck_joyx();
joyy=nunchuck_joyy();
//nunchuck_print_data();
//Decide
the position where the joystick is
if(joyx<80 data-blogger-escaped-span="">
{
if(joyy<80 data-blogger-escaped-pos="6;</span">
else
if(joyy>200) pos=8;
else
pos=7;
}
else
if(joyx>170)
{
if(joyy>200) pos=2;
else
if(joyy<80 data-blogger-escaped-pos="4;</span">
else
pos=3;
}
else
{
if(joyy>220) pos=1;
else
if(joyy<50 data-blogger-escaped-pos="5;</span">
else
pos=0;
}
Serial.println(pos);
delay(100);
}