The 511 along z may just be the acceleration due to gravity, which you will always be measuring.
There is a fair amount of junk code in that program that is actually obscuring what the device is measuring.
I removed that junk. Try the following and while the program is running, orient the accelerometer in different directions. (I don't have an ADXL345 to test).
#include <Wire.h>
#define accel_module (0x53)
byte values[6];
char output[50]; //sprintf buffer
void setup() {
Wire.begin();
Serial.begin(115200);
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(16);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
Serial.println("Restarted");
}
void loop() {
int xyzregister = 0x32;
int x,y,z;
Wire.beginTransmission(accel_module);
Wire.write(xyzregister);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.requestFrom(accel_module, 6);
int i=0;
while(Wire.available())
{
values[i] = Wire.read();
i++;
}
Wire.endTransmission();
if (i < 5) Serial.println("too few values returned");
x = (((int)values[1]) << 8) | values[0];
y = (((int)values[3]) << 8) | values[2];
z = (((int)values[5]) << 8) | values[4];
sprintf(output, "%d %d %d", x, y, z);
Serial.println(output);
delay(100);
}
Actually, the code was not junk - it was meant to write to serial only in certain angles' jumps and it works great.
I just tried your code - it behaves the same - no change in output of Z which is always 511 no matter what - even if I move it fast from high point to bottom, so I don't believe it is a gravity issue that holds it with this value.
when the ADXL345 text on the module is facing to me, tilting forward makes Y go higher (backward = lower),
tilting right makes X go higher (left = lower).
that's the reason i used x+80, y+257 in the code i've posted - in order to make the x and y equal to 0 when it is resting straight on the table. this way it displayed the rotation correctly.
by not doing so - the readings are (when the module is "resting" on the table) x=-257 and y=-80 (approx.)
i didn't understand what you meant by: "does the module have 5V to 3.3V level shifters on all connectors".
i'll check the values when pointing at these points and post it back - in what way can it matter to the problem? do you have any lead?