I have got a running wired nunchuk to set postion and parameters of my panorama robot
next step should be to take a wireless nunchuk to get rid of the cable
i am trying to connect it with my arduino but without success. even when nothing is connected the wiring functions return values like
get
read:
0100:read Z
get
read:
0100:read Z
get
read:
0100:read
this is my sketch:
has anybody an idea what i am doing wrong?
#include <SoftwareSerial.h>
#include <Wire.h>
// NUNCHUK (Pins: A4 green, A5 yellow
static uint8_t nunchuck_buf[6]; // array to store nunchuck data
// nunchuck X Y
int NUN_X;
int NUN_Y;
// nunchuk buttons
int NUN_C;
int NUN_Z;
//-------------------------
void setup() {
// init nunchuck
Serial.begin(19200);
nunchuck_init(); // send the initilization handshake
delay(200);
//nunchuck_zero();
nunchuck_get_data();
delay(200);
Serial.println("Setup done");
}
//----------------------
void get_params()
{
while(1)
{
//nunchuck_zero();
Serial.println("get ");
delay(200);
nunchuck_read();
if (NUN_C == 1) Serial.println("C ");
if (NUN_Z == 1) Serial.println("Z ");
}
delay(1000);
Serial.println("DONE");
delay(200);
}
//
//-------------------------------------------------------------
//
void loop()
{
// get paramters via nunchuck
get_params();
Serial.println("XXX DONE XXX");
delay(1000);
}
//----------------------------------------------------------------------
//
// Nunchuck functions
//
// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
void nunchuck_init()
{
Wire.begin(); // join i2c bus as master
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 nunchuck_send_request() // Send a request for data to the nunchuck
{
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.send(0x00); // sends one byte
Wire.endTransmission(); // stop transmitting
}
//---------
int nunchuck_get_data() // Receive data back from the nunchuck,
{
int cnt=0;
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.receive());
cnt++;
}
nunchuck_send_request(); // send request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
//---------
void nunchuck_read()
{
NUN_C = 0;
NUN_Z = 0;
NUN_X = 0;
NUN_Y = 0;
while (nunchuck_get_data() == 0);
NUN_C = nunchuck_cbutton();
NUN_Z = nunchuck_zbutton();
NUN_X = nunchuck_joyx();
NUN_Y = nunchuck_joyy();
Serial.println("read:");
Serial.print(NUN_C,DEC);
Serial.print(NUN_Z,DEC);
Serial.print(NUN_X,DEC);
Serial.print(NUN_Y,DEC);
Serial.print(":read ");
delay(1000);
}
//---------
// 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;
}
// returns zbutton state: 1=pressed, 0=notpressed
int nunchuck_zbutton()
{
return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1; // voodoo
}
// returns zbutton state: 1=pressed, 0=notpressed
int nunchuck_cbutton()
{
return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1; // voodoo
}
// returns mapped value of x-axis joystick
int nunchuck_joyx()
{
int r;
r = nunchuck_buf[0];
return nunxymap(r);
}
// returns mapped value of y-axis joystick
int nunchuck_joyy()
{
int r;
r = nunchuck_buf[1];
return nunxymap(r);
}
// map val to a range of -1, 0, 1
int nunxymap(int val)
{
int r1;
r1 = 0;
// map r1 to -1, 0, 1
if ( val < 10) r1 = -1;
else if ( val > 240) r1 = 1;
if (NUN_C == 1) r1 *= 10; // x, y * 10, if C pressed
return r1;
}
//--------