Arduino and nunchuck as a tilt sensor

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,

#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()
  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;


    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("  accy: "); 
    Serial.print("  accz: "); 
    Serial.print("  zbut: "); 
    Serial.print("  cbut: "); 
    Serial.print("pitch: ");

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.

Try this:

accx = ((double)(countx-offsetX)/scaleX);

And the same for the rest....

didnt work, but tank you

I found the error, I just change the constant from int to double, and it works