WiiChuck Controlling
Use the Nunchuck from a Wii console to control lights and sounds!
Components and supplies
LED (generic)
Jumper wires (generic)
Wii Nunchuck
Wii Chuck Adapter
Resistor 221 ohm
Speaker: 0.25W, 8 ohms
Breadboard (generic)
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
WiiChuck Lights + Sounds
c_cpp
Nunchuck Sensing and such: When the joystick is moved up, the blue light turns on When the z button is pressed, the red light turns on When the c button is pressed, the white light turns on.
1#include <Wire.h> 2 3void setup(){ 4Serial.begin(19200); 5nunchuck_setpowerpins(); 6nunchuck_init(); 7Serial.print("Nunchuck ready\ 8"); 9 10pinMode(9, OUTPUT); 11pinMode(10, OUTPUT); 12pinMode(11, OUTPUT); 13} 14 15/*int joy_x_axis = nunchuck_buf[0]; 16int joy_y_axis = nunchuck_buf[1]; 17int accel_x_axis = nunchuck_buf[2]; // * 2 * 2; 18int accel_y_axis = nunchuck_buf[3]; // * 2 * 2; 19int accel_z_axis = nunchuck_buf[4]; // * 2 * 2; 20int z_button = 0; 21int c_button = 0;**/ 22 23void loop(){ 24nunchuck_get_data(); 25nunchuck_print_data(); 26 27delay(20); 28} 29//======================================================================================================================================================================================================// 30//Do not modify!!!!!!!! 31//======================================================================================================================================================================================================// 32 33 34// 35// Nunchuck functions 36// 37 38static uint8_t nunchuck_buf[6]; // array to store nunchuck data, 39 40// Uses port C (analog in) pins as power & ground for Nunchuck 41static void nunchuck_setpowerpins() 42{ 43#define pwrpin PC3 44#define gndpin PC2 45DDRC |= _BV(pwrpin) | _BV(gndpin); 46PORTC &=~ _BV(gndpin); 47PORTC |= _BV(pwrpin); 48delay(100); // wait for things to stabilize 49} 50 51// initialize the I2C system, join the I2C bus, 52// and tell the nunchuck we're talking to it 53void nunchuck_init() 54{ 55Wire.begin(); // join i2c bus as master 56Wire.beginTransmission(0x52); // transmit to device 0x52 57Wire.write(0x40); // sends memory address 58Wire.write(0x00); // sends sent a zero. 59Wire.endTransmission(); // stop transmitting 60} 61 62// Send a request for data to the nunchuck 63// was "send_zero()" 64void nunchuck_send_request() 65{ 66Wire.beginTransmission(0x52); // transmit to device 0x52 67Wire.write(0x00); // sends one byte 68Wire.endTransmission(); // stop transmitting 69} 70 71// Receive data back from the nunchuck, 72// returns 1 on successful read. returns 0 on failure 73int nunchuck_get_data() 74{ 75int cnt=0; 76Wire.requestFrom (0x52, 6); // request data from nunchuck 77while (Wire.available ()) { 78// receive byte as an integer 79nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read()); 80cnt++; 81} 82nunchuck_send_request(); // send request for next data payload 83// If we recieved the 6 bytes, then go print them 84if (cnt >= 5) { 85return 1; // success 86} 87return 0; //failure 88} 89 90// Print the input data we have recieved 91// accel data is 10 bits long 92// so we read 8 bits, then we have to add 93// on the last 2 bits. That is why I 94// multiply them by 2 * 2 95void nunchuck_print_data() 96{ 97static int i=0; 98int joy_x_axis = nunchuck_buf[0]; 99int joy_y_axis = nunchuck_buf[1]; 100int accel_x_axis = nunchuck_buf[2]; // * 2 * 2; 101int accel_y_axis = nunchuck_buf[3]; // * 2 * 2; 102int accel_z_axis = nunchuck_buf[4]; // * 2 * 2; 103 104int z_button = 0; 105int c_button = 0; 106 107// byte nunchuck_buf[5] contains bits for z and c buttons 108// it also contains the least significant bits for the accelerometer data 109// so we have to check each bit of byte outbuf[5] 110if ((nunchuck_buf[5] >> 0) & 1) 111z_button = 1; 112if ((nunchuck_buf[5] >> 1) & 1) 113c_button = 1; 114 115if ((nunchuck_buf[5] >> 2) & 1) 116accel_x_axis += 2; 117if ((nunchuck_buf[5] >> 3) & 1) 118accel_x_axis += 1; 119 120if ((nunchuck_buf[5] >> 4) & 1) 121accel_y_axis += 2; 122if ((nunchuck_buf[5] >> 5) & 1) 123accel_y_axis += 1; 124 125if ((nunchuck_buf[5] >> 6) & 1) 126accel_z_axis += 2; 127if ((nunchuck_buf[5] >> 7) & 1) 128accel_z_axis += 1; 129 130 131tone(3, accel_y_axis + 150); 132 133if (z_button == 0) { 134 digitalWrite(9, HIGH); 135} else { 136 digitalWrite(9, LOW); 137} 138 139if (joy_y_axis > 199) { 140 digitalWrite(10, HIGH); 141} else { 142 digitalWrite(10, LOW); 143} 144 145if (c_button == 0) { 146 digitalWrite(11, HIGH); 147} else { 148 digitalWrite(11, LOW); 149} 150 151 152Serial.print(i,DEC); 153Serial.print("\ "); 154 155Serial.print("joy:"); 156Serial.print(joy_x_axis,DEC); 157Serial.print(","); 158Serial.print(joy_y_axis, DEC); 159Serial.print(" \ "); 160 161Serial.print("acc:"); 162Serial.print(accel_x_axis, DEC); 163Serial.print(","); 164Serial.print(accel_y_axis, DEC); 165Serial.print(","); 166Serial.print(accel_z_axis, DEC); 167Serial.print("\ "); 168 169Serial.print("but:"); 170Serial.print(z_button, DEC); 171Serial.print(","); 172Serial.print(c_button, DEC); 173 174Serial.print("\ \ 175"); // newline 176i++; 177} 178 179// Encode data to format that most wiimote drivers except 180// only needed if you use one of the regular wiimote drivers 181char nunchuk_decode_byte (char x) 182{ 183x = (x ^ 0x17) + 0x17; 184return x; 185} 186 187// returns zbutton state: 1=pressed, 0=notpressed 188int nunchuck_zbutton() 189{ 190return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1; 191} 192 193// returns zbutton state: 1=pressed, 0=notpressed 194int nunchuck_cbutton() 195{ 196return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1; 197} 198 199// returns value of x-axis joystick 200int nunchuck_joyx() 201{ 202return nunchuck_buf[0]; 203} 204 205// returns value of y-axis joystick 206int nunchuck_joyy() 207{ 208return nunchuck_buf[1]; 209} 210 211// returns value of x-axis accelerometer 212int nunchuck_accelx() 213{ 214return nunchuck_buf[2]; // FIXME: this leaves out 2-bits of the data 215} 216 217// returns value of y-axis accelerometer 218int nunchuck_accely() 219{ 220return nunchuck_buf[3]; // FIXME: this leaves out 2-bits of the data 221} 222 223// returns value of z-axis accelerometer 224int nunchuck_accelz() 225{ 226return nunchuck_buf[4]; // FIXME: this leaves out 2-bits of the data 227}
WiiChuck Lights + Sounds
c_cpp
Nunchuck Sensing and such: When the joystick is moved up, the blue light turns on When the z button is pressed, the red light turns on When the c button is pressed, the white light turns on.
1#include <Wire.h> 2 3void setup(){ 4Serial.begin(19200); 5nunchuck_setpowerpins(); 6nunchuck_init(); 7Serial.print("Nunchuck 8 ready\ 9"); 10 11pinMode(9, OUTPUT); 12pinMode(10, OUTPUT); 13pinMode(11, OUTPUT); 14} 15 16/*int 17 joy_x_axis = nunchuck_buf[0]; 18int joy_y_axis = nunchuck_buf[1]; 19int accel_x_axis 20 = nunchuck_buf[2]; // * 2 * 2; 21int accel_y_axis = nunchuck_buf[3]; // * 2 * 2; 22int 23 accel_z_axis = nunchuck_buf[4]; // * 2 * 2; 24int z_button = 0; 25int c_button 26 = 0;**/ 27 28void loop(){ 29nunchuck_get_data(); 30nunchuck_print_data(); 31 32delay(20); 33} 34//======================================================================================================================================================================================================// 35//Do 36 not modify!!!!!!!! 37//======================================================================================================================================================================================================// 38 39 40// 41// 42 Nunchuck functions 43// 44 45static uint8_t nunchuck_buf[6]; // array to store 46 nunchuck data, 47 48// Uses port C (analog in) pins as power & ground for Nunchuck 49static 50 void nunchuck_setpowerpins() 51{ 52#define pwrpin PC3 53#define gndpin PC2 54DDRC 55 |= _BV(pwrpin) | _BV(gndpin); 56PORTC &=~ _BV(gndpin); 57PORTC |= _BV(pwrpin); 58delay(100); 59 // wait for things to stabilize 60} 61 62// initialize the I2C system, join the 63 I2C bus, 64// and tell the nunchuck we're talking to it 65void nunchuck_init() 66{ 67Wire.begin(); 68 // join i2c bus as master 69Wire.beginTransmission(0x52); // transmit to device 70 0x52 71Wire.write(0x40); // sends memory address 72Wire.write(0x00); // sends 73 sent a zero. 74Wire.endTransmission(); // stop transmitting 75} 76 77// Send 78 a request for data to the nunchuck 79// was "send_zero()" 80void nunchuck_send_request() 81{ 82Wire.beginTransmission(0x52); 83 // transmit to device 0x52 84Wire.write(0x00); // sends one byte 85Wire.endTransmission(); 86 // stop transmitting 87} 88 89// Receive data back from the nunchuck, 90// returns 91 1 on successful read. returns 0 on failure 92int nunchuck_get_data() 93{ 94int 95 cnt=0; 96Wire.requestFrom (0x52, 6); // request data from nunchuck 97while (Wire.available 98 ()) { 99// receive byte as an integer 100nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read()); 101cnt++; 102} 103nunchuck_send_request(); 104 // send request for next data payload 105// If we recieved the 6 bytes, then go 106 print them 107if (cnt >= 5) { 108return 1; // success 109} 110return 0; //failure 111} 112 113// 114 Print the input data we have recieved 115// accel data is 10 bits long 116// so 117 we read 8 bits, then we have to add 118// on the last 2 bits. That is why I 119// 120 multiply them by 2 * 2 121void nunchuck_print_data() 122{ 123static int i=0; 124int 125 joy_x_axis = nunchuck_buf[0]; 126int joy_y_axis = nunchuck_buf[1]; 127int accel_x_axis 128 = nunchuck_buf[2]; // * 2 * 2; 129int accel_y_axis = nunchuck_buf[3]; // * 2 * 2; 130int 131 accel_z_axis = nunchuck_buf[4]; // * 2 * 2; 132 133int z_button = 0; 134int c_button 135 = 0; 136 137// byte nunchuck_buf[5] contains bits for z and c buttons 138// it also 139 contains the least significant bits for the accelerometer data 140// so we have 141 to check each bit of byte outbuf[5] 142if ((nunchuck_buf[5] >> 0) & 1) 143z_button 144 = 1; 145if ((nunchuck_buf[5] >> 1) & 1) 146c_button = 1; 147 148if ((nunchuck_buf[5] 149 >> 2) & 1) 150accel_x_axis += 2; 151if ((nunchuck_buf[5] >> 3) & 1) 152accel_x_axis 153 += 1; 154 155if ((nunchuck_buf[5] >> 4) & 1) 156accel_y_axis += 2; 157if ((nunchuck_buf[5] 158 >> 5) & 1) 159accel_y_axis += 1; 160 161if ((nunchuck_buf[5] >> 6) & 1) 162accel_z_axis 163 += 2; 164if ((nunchuck_buf[5] >> 7) & 1) 165accel_z_axis += 1; 166 167 168tone(3, 169 accel_y_axis + 150); 170 171if (z_button == 0) { 172 digitalWrite(9, HIGH); 173} 174 else { 175 digitalWrite(9, LOW); 176} 177 178if (joy_y_axis > 199) { 179 digitalWrite(10, 180 HIGH); 181} else { 182 digitalWrite(10, LOW); 183} 184 185if (c_button == 0) { 186 187 digitalWrite(11, HIGH); 188} else { 189 digitalWrite(11, LOW); 190} 191 192 193Serial.print(i,DEC); 194Serial.print("\ "); 195 196Serial.print("joy:"); 197Serial.print(joy_x_axis,DEC); 198Serial.print(","); 199Serial.print(joy_y_axis, 200 DEC); 201Serial.print(" \ "); 202 203Serial.print("acc:"); 204Serial.print(accel_x_axis, 205 DEC); 206Serial.print(","); 207Serial.print(accel_y_axis, DEC); 208Serial.print(","); 209Serial.print(accel_z_axis, 210 DEC); 211Serial.print("\ "); 212 213Serial.print("but:"); 214Serial.print(z_button, 215 DEC); 216Serial.print(","); 217Serial.print(c_button, DEC); 218 219Serial.print("\ \ 220"); 221 // newline 222i++; 223} 224 225// Encode data to format that most wiimote drivers 226 except 227// only needed if you use one of the regular wiimote drivers 228char nunchuk_decode_byte 229 (char x) 230{ 231x = (x ^ 0x17) + 0x17; 232return x; 233} 234 235// returns zbutton 236 state: 1=pressed, 0=notpressed 237int nunchuck_zbutton() 238{ 239return ((nunchuck_buf[5] 240 >> 0) & 1) ? 0 : 1; 241} 242 243// returns zbutton state: 1=pressed, 0=notpressed 244int 245 nunchuck_cbutton() 246{ 247return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1; 248} 249 250// 251 returns value of x-axis joystick 252int nunchuck_joyx() 253{ 254return nunchuck_buf[0]; 255} 256 257// 258 returns value of y-axis joystick 259int nunchuck_joyy() 260{ 261return nunchuck_buf[1]; 262} 263 264// 265 returns value of x-axis accelerometer 266int nunchuck_accelx() 267{ 268return nunchuck_buf[2]; 269 // FIXME: this leaves out 2-bits of the data 270} 271 272// returns value of y-axis 273 accelerometer 274int nunchuck_accely() 275{ 276return nunchuck_buf[3]; // FIXME: 277 this leaves out 2-bits of the data 278} 279 280// returns value of z-axis accelerometer 281int 282 nunchuck_accelz() 283{ 284return nunchuck_buf[4]; // FIXME: this leaves out 2-bits 285 of the data 286}
Downloadable files
Lights + Sound Diagram
How to link 3 LEDs and a speaker to an Arduino
Lights + Sound Diagram
Comments
Only logged in users can leave comments