ADXL335 accelerometer not detecting angles properly

I'm using the ADXL335 accelerometer to detect angles, but no matter how much I tilt my sensor, the detected angles shown on the serial monitor have little to no changes.Even if i don't move the sensor at all, the detected angles are constantly changing(but within a small range). Here I attached my codes.

CODES:

//
ADXL3xx

Reads an Analog Devices ADXL3xx accelerometer and communicates the
acceleration to the computer. The pins used are designed to be easily
compatible with the breakout boards from SparkFun, available from:

The circuit:

  • analog 0: accelerometer self test
  • analog 1: z-axis
  • analog 2: y-axis
  • analog 3: x-axis
  • analog 4: ground
  • analog 5: vcc

created 2 Jul 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

// by Zhaorui, to turn xyz accel into angles:
// Simple angle meter using ADXL335 accelerometer
// from electronicsblog.net/
// Youtube demoļ¼šSimple angle meter using Arduino and ADXL335 accelerometer (more info in video description) - YouTube
// code ref: Simple angle meter using ADXL335 accelerometer [Arduino] | electronicsblog.net

#if defined(AVR_ATtiny24) || defined(AVR_ATtiny44) || defined(AVR_ATtiny84) || defined(AVR_ATtiny25) || defined(AVR_ATtiny45) || defined(AVR_ATtiny85)
#define DEFAULT 0
#define EXTERNAL 1
#define INTERNAL 2
#else
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560) || defined(AVR_ATmega1284) || defined(AVR_ATmega1284P) || defined(AVR_ATmega644) || defined(AVR_ATmega644A) || defined(AVR_ATmega644P) || defined(AVR_ATmega644PA)
#define INTERNAL1V1 2
#define INTERNAL2V56 3
#else
#define INTERNAL 3
#endif
#define DEFAULT 1
#define EXTERNAL 0
#endif

// these constants describe the pins. They won't change:
//const int groundpin = 18; // analog input pin 4 -- ground
//const int powerpin = 19; // analog input pin 5 -- voltage
//const int xpin = A3; // x-axis of the accelerometer
//const int ypin = A2; // y-axis
//const int zpin = A1; // z-axis (only on 3-axis models)

// by Zhaorui:changed to adapt to the spec board;
const int powerpin = A5; // analog input pin 5 -- voltage
const int xpin = A4; // x-axis of the accelerometer
const int ypin = A3; // y-axis
const int zpin = A2; // z-axis (only on 3-axis models)
const int groundpin = A1; // 18: // analog input pin 4 -- ground

// by zhaorui:
//To make this program not only sends readings of acceleration from 3 axis via
//serial port, but also calculates rotation of the accelerometer around X, Y, Z, axis.
//
//#define ADC_ref 2.56
//#define zero_x 1.569
//#define zero_y 1.569
//#define zero_z 1.569
//#define sensitivity_x 0.3
//#define sensitivity_y 0.3
//#define sensitivity_z 0.3
#define ADC_ref 3.29
#define zero_x 3.05
#define zero_y 3.05
#define zero_z 3.05
#define sensitivity_x 0.3
#define sensitivity_y 0.3
#define sensitivity_z 0.3

void setup() {
// initialize the serial communications:
Serial.begin(9600);

// Provide ground and power by using the analog inputs as normal digital pins.
// This makes it possible to directly connect the breakout board to the
// Arduino. If you use the normal 5V and GND pins on the Arduino,
// you can remove these lines.
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);

// by zhaorui: for calculating angle:
// I use a number instead of as for Arduino MEGA. See the forum : Arduino Nano undocumented feature analogReference(0) - Programming Questions - Arduino Forum
//analogReference(INTERNAL2V56);
analogReference(1);
}

void loop() {
calculateAngle();
delay(1000);
}

void calculateAngle()
{
unsigned int value_x = analogRead(xpin);
unsigned int value_y = analogRead(ypin);
unsigned int value_z = analogRead(zpin);

float xv;
float yv;
float zv;

float angle_x;
float angle_y;
float angle_z;

// print the sensor values:
Serial.print(value_x);
// print a tab between values:
Serial.print("\t");
Serial.print(value_y);
// print a tab between values:
Serial.print("\t");
Serial.print(value_z);
Serial.println();

xv=(value_x/1024.0*ADC_ref-zero_x)/sensitivity_x;

Serial.print ("x= ");
Serial.print (xv);
Serial.print(" g ");

yv=(value_y/1024.0*ADC_ref-zero_y)/sensitivity_y;

Serial.print ("y= ");
Serial.print (yv);
Serial.print(" g ");

zv=(value_z/1024.0*ADC_ref-zero_z)/sensitivity_z;

Serial.print ("z= ");
Serial.print (zv);
Serial.print(" g ");
Serial.print("\n");
Serial.print("Rotation ");
Serial.print("x= ");

angle_x =atan2(-yv,-zv)*57.2957795+180;

Serial.print(angle_x);

Serial.print(" deg");
Serial.print(" ");
Serial.print("y= ");

angle_y =atan2(-xv,-zv)*57.2957795+180;

Serial.print(angle_y);
Serial.print(" deg");
Serial.print(" ");

Serial.print("z= ");

angle_z =atan2(-yv,-xv)*57.2957795+180;

Serial.print(angle_z);
Serial.print(" deg");
Serial.print("\n");

delay(1000);

}

An accelerometer detects acceleration, not angles. In my opinion You are using the wrong kind of sensor to detect angles.

Please edit your post to add codes tags, as described in the "How to use this forum" post.

You can measure only two tilt angles using an accelerometer, and you are not using the correct formulas. See this page for the correct ones.