if (chuck.buttonZ) {
Serial.print("Z");
} else {
Serial.print("-");
}
Serial.print(", ");
//not a function// if (chuck.buttonC()) {
if (chuck.buttonC) {
Serial.print("C");
} else {
Serial.print("-");
}
Serial.println();
}
can anyone tell me how to connect the pins of the wireless dongle for the chuck into my uno? they don't seem to be defined in this code...? I'm confused #noob
okay, so this webpage has the three files that I need apparently, but im quite nooby so how do i turn these into class files or libraries or whatever I need to make the code posted above work and find out the pin connections?
To wire up the nunchuck you need something like this break out board so you can get the connections into the Arduino.
You use the I2C bus to talk to it. Data is the A4 pin and Clock is the A5 pin on a Uno.
But this is a bit complicated because the Arduino has a 5V system and the nunchuck uses 3V3. The standard I2C library enables internal pull up resistors on the I2C lines so you have to use another library that is designed to have its pull up resistors disabled.
This is one such:-
Then you need to connect two external pull up resistors, one between A5 and the 3V3 and the other between A4 and the 3V3.
Once you have done this ( and connected the ground and the 3V3 power ) you are ready to program it.
You do not need to have a library specifically for the nunchuck, you can just give it the appropriate I2C commands. This is a small sample code to do this.
// Nunchuck test
// By Mike Cook
#include <I2C.h>
int outbuf[6]; // array to store results
void setup () {
Serial.begin (19200);
I2c.begin();
I2c.pullup(0); // disable I2C pull ups
I2c.timeOut(500); // half a second to prevent lock up
Serial.print ("Finished setup\n");
nunchuck_init (); // send the initialisation handshake
}
void nunchuck_init () {
// I2c.write(address, registerAddress, data) // set up start of read
I2c.write(0x52, 0x40, 0x00);
}
void send_zero () {
I2c.write(0x52, 0x00);
}
void loop () {
// I2c.read(address, numberBytes)
Serial.println(" ");
Serial.println("Raw data");
I2c.read(0x52, 6);
for(int j=0; j<6; j++){ // now get the bytes one at a time
outbuf[j] = I2c.receive(); // receive a byte
if(outbuf[j] < 0x10) Serial.print("0"); // print leading zero if needed
Serial.print(outbuf[j], HEX); // print the byte
Serial.print(" ");
}
Serial.println(" "); // new line
printResults();
send_zero (); // send the request for next bytes
delay (800);
}
// Print the input data we have received
// accel data is 10 bits long
// so we read 8 bits and the LS bits are in the last byte
void printResults ()
{
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]
z_button = outbuf[5] & 1;
c_button = 1 ^ ((outbuf[5] >> 1) & 1) ^ (outbuf[5] & 1);
outbuf[2] = (outbuf[2] << 2) | (outbuf[5] >> 2) & 0x3; // acc x
outbuf[3] = (outbuf[3] << 2) | (outbuf[5] >> 4) & 0x3; // acc y
outbuf[4] = (outbuf[4] << 2) | (outbuf[5] >> 6) & 0x3; // acc x
for(int i=0; i<5; i++){
Serial.print (outbuf[i], DEC);
Serial.print ("\t");
}
Serial.print (z_button, DEC);
Serial.print ("\t");
Serial.print (c_button, DEC);
Serial.print ("\t");
Serial.println(" ");
}
In your sketch folder, create a folder called 'libraries' if it does not exist.
Inside the 'libraries' folder create a folder called 'WiiChuck'.
Inside the 'WiiChuck' folder put the file WiiChuck.h: http://playground.arduino.cc/Main/WiiChuckClass?action=sourceblock&num=2
Inside the 'WiiChuck' folder create a folder called 'examples'.
Inside the 'examples' folder create a folder called 'demo'.
Inside the 'demo' folder place the file demo.ino: Arduino Playground - WiiChuckClass
Re-start the IDE. Load the sketch File->Examples->WiiChuck->demo and try to verify it (leftmost button in the header of the sketch).