Hey guys, I’m trying to read an angle using the nunchuck. The connection between the nunchuck and arduino seems OK. I can read the acceleration as a byte number, something like 185. To convert to G or m/s2 I made some calibration.
accx = (double)((countx-offsetX)/scaleX);
accy = (double)((county-offsetY)/scaleY);
accz = (double)((countz-offsetZ)/scaleZ+1);
this code give me the acceleration in G, if I need in m/s2 just multiply by 9.81.
This is the full code, a little modification from Tod E. Kurt. (Tank you Tod)
/*
* WiiChuckDemo --
*
* 2008 Tod E. Kurt, http://thingm.com/
*
*/
#include "nunchuck_funcs.h"
#include "WProgram.h"
#include <Wire.h>
#include <math.h>
int loop_cnt=0;
int zbut,cbut;
int ledPin = 13;
int offsetX=134,offsetY=115,offsetZ=185; //offset values are obtained holding the chuck on a fix zero position
int scaleX=-50,scaleY=50,scaleZ=50; //point axxis upward then downward, subtracting the values, and divide by 2
double accx,accy,accz;
byte countx,county,countz;
double pitch;
void setup()
{
Serial.begin(19200);
nunchuck_setpowerpins();
nunchuck_init(); // send the initilization handshake
Serial.print("WiiChuckDemo ready\n");
}
void loop()
{
if( loop_cnt > 100 ) { // every 100 msecs get new data
loop_cnt = 0;
nunchuck_get_data();
countx = nunchuck_accelx(); // ranges from approx 70 - 182
county = nunchuck_accely(); // ranges from approx 65 - 173
countz = nunchuck_accelz(); // ranges from approx -
zbut = nunchuck_zbutton();
cbut = nunchuck_cbutton();
accx = (double)((countx-offsetX)/scaleX);
accy = (double)((county-offsetY)/scaleY);
accz = (double)((countz-offsetZ)/scaleZ+1);
pitch = (double)(atan2(accy,accz))*180/3.14;
Serial.print("accx: ");
Serial.print(accx);
delay(1);
Serial.print(" accy: ");
Serial.print(accy);
delay(1);
Serial.print(" accz: ");
Serial.print(accz);
delay(1);
Serial.print(" zbut: ");
Serial.print(zbut);
delay(1);
Serial.print(" cbut: ");
Serial.println(cbut);
delay(1);
Serial.print("pitch: ");
Serial.println(pitch);
//nunchuck_print_data();
}
loop_cnt++;
delay(1);
}
But when I output the result, the pitch angle, I only see when the nunchuck is on 0º,45º,90º degree position.
I cant see the intermediary position.
If someone can help me.
Sorry my english, and some mistake I had made.
TY