Pitch and roll angles from ADXL345

How do you calculate the pitch and roll angles from the x, y and z from the ADXL345.

I have tried using this code from here http://www.den-uijl.nl/electronics/gyro.html

void loop() {
  timer = millis();
  getGyroscopeReadings(gyroResult);
  getAccelerometerReadings(accelResult);
  
  pitchAccel = atan2((accelResult[1] - biasAccelY) / 256, (accelResult[2] - biasAccelZ) / 256) * 360.0 / (2*PI);
  pitchGyro = pitchGyro + ((gyroResult[0] - biasGyroX) / 14.375) * timeStep;
  pitchPrediction = pitchPrediction + ((gyroResult[0] - biasGyroX) / 14.375) * timeStep;
  
  rollAccel = atan2((accelResult[0] - biasAccelX) / 256, (accelResult[2] - biasAccelZ) / 256) * 360.0 / (2*PI);
  rollGyro = rollGyro - ((gyroResult[1] - biasGyroY) / 14.375) * timeStep; 
  rollPrediction = rollPrediction - ((gyroResult[1] - biasGyroY) / 14.375) * timeStep;
  
  Pxx += timeStep * (2 * Pxv + timeStep * Pvv);
  Pxv += timeStep * Pvv;
  Pxx += timeStep * giroVar;
  Pvv += timeStep * deltaGiroVar;
  kx = Pxx * (1 / (Pxx + accelVar));
  kv = Pxv * (1 / (Pxx + accelVar));
  
  pitchPrediction += (pitchAccel - pitchPrediction) * kx;
  rollPrediction += (rollAccel - rollPrediction) * kx;
  
  Pxx *= (1 - kx);
  Pxv *= (1 - kx);
  Pvv -= kv * Pxv;
  
  Serial.print(pitchGyro);
  Serial.print("\t");
  Serial.print(pitchAccel);
  Serial.print("\t");
  Serial.print(pitchPrediction);
  Serial.print("\t"); 
  Serial.print(rollGyro);
  Serial.print("\t");
  Serial.print(rollAccel);
  Serial.print("\t");
  Serial.print(rollPrediction);
  Serial.print("\n");
  
  timer = millis() - timer;
  timer = (timeStep * 1000) - timer; 
  delay(timer);
}

But it doesn't seem to work - I am getting 0 or 1 for the angles.

Here is my code:

void Adafruit_ADXL345_Unified::getAngles(sensors_event_t &event, uint16_t &nPitchAngle, uint16_t &nRollAngle)
{
  float fPitch = 0.0, fRoll = 0.0;
  
  fPitch = atan2((event.acceleration.y - m_fZeroY) / 256, (event.acceleration.z - m_fZeroZ) / 256) * 360.0 / (2*PI);
  fRoll = atan2((event.acceleration.x - m_fZeroX) / 256, (event.acceleration.z - m_fZeroZ) / 256) * 360.0 / (2*PI);
   nPitchAngle = fPitch;
  nRollAngle = fRoll;
}

I have tried using this code

That won't even compile. The fine folks at http://snippets-r-us.com are experts at dealing with snippets.

PaulS:
That won't even compile. The fine folks at http://snippets-r-us.com are experts at dealing with snippets.

It is the calculations that I am concerned with here.

How do you calculate the roll and pitch from the raw data from the ADXL234?

Are the formulas used in that code snippet correct for the ADXL345?

It is the calculations that I am concerned with here.

So am I. But, I can't tell if you are performing integer arithmetic and expecting other than integer results.

How do you calculate the roll and pitch from the raw data from the ADXL234?

I don't. I don't need to.

Are the formulas used in that code snippet correct for the ADXL345?

Are you getting the right results? If not, then, clearly, the answer is no.

This is the definitive guide to pitch and roll calculations. Depending on sensor orientation, use either eqns 25 and 26, or 28 and 29.

Note: in the formula below, there is no need to divide by 256. atan2() takes care of the scaling.

atan2((accelResult[1] - biasAccelY) / 256, (accelResult[2] - biasAccelZ) / 256)

I found this website that explains it better:

http://theccontinuum.com/2012/09/24/arduino-imu-pitch-roll-from-accelerometer/