Tilt underwater

Thanks jremington, it is now nearly working. Still something wrong at the end though.

The cos value of tilt looks realistic but the conversion to degrees is wrong. This is surely something due to my beginner status with code.

Here is my latest code.

/*  Uses I2C comms with 2 x 4.7K resistors 
 *  Uses sparkfun ADXL345 library
 *  This one applys Offset and gain values derived by calibration
 *  Outputs G values which are then used with freescale equation 50 to get tilt
 *  Offset and Gain values are when set to +or- 2g mode.
 *  G values mapped for vertical use

*/
#include <SparkFun_ADXL345.h>     


//*********** COMMUNICATION SELECTION ***********/
/*    Comment Out The One You Are Not Using    */
//ADXL345 adxl = ADXL345(10);           // USE FOR SPI COMMUNICATION, ADXL345(CS_PIN);
ADXL345 adxl = ADXL345();             // USE FOR I2C COMMUNICATION


//****************** VARIABLES ******************/
/*                                             */
float Xo;
float Yo;
float Zo;

float Xos;
float Yos;
float Zos;

float Xmap;
float Ymap;
float Zmap;

float Modmove;

float Costilt;
float Tilt;


//************** DEFINED VARIABLES **************/
/*                                             */
#define offsetX   14       // OFFSET values
#define offsetY   2
#define offsetZ   -11

#define gainX     260        // GAIN factors
#define gainY     264
#define gainZ     253 

#define Xvert     0.001
#define Yvert     0.001
#define Zvert     1.001
#define Modvert   1.001

//******************** SETUP ********************/
/*          Configure ADXL345 Settings         */
void setup(){
  
  Serial.begin(9600);                 // Start the serial terminal
  
  adxl.powerOn();                     // Power on the ADXL345

  adxl.setRangeSetting(2);           // Give the range settings
                                      // Accepted values are 2g, 4g, 8g or 16g
                                      // Higher Values = Wider Measurement Range
                                      // Lower Values = Greater Sensitivity

  //adxl.setSpiBit(0);                  // Configure the device to be in 4 wire SPI mode when set to '0' or 3 wire SPI mode when set to 1
                                      // Default: Set to 1
                                      // SPI pins on the ATMega328: 11, 12 and 13 as reference in SPI Library 
                                      // Preseume not needed in I2C mode
}



//****************** MAIN CODE ******************/
/*     Accelerometer Readings and Interrupt    */
void loop(){


    // Get the Accelerometer Readings

    int x,y,z; 
       
    adxl.readAccel(&x, &y, &z);         // Read the accelerometer values and store them in variables declared above x,y,z
           
    Serial.print("x y z: "); Serial.print(x); Serial.print("  ");Serial.print(y); Serial.print("  "); Serial.print(z);

  
    //Apply offsets
  Xo = (x - offsetX);         // Calculating New Values for X, Y and Z with offsets applied
  Yo = (y - offsetY);
  Zo = (z - offsetZ);
  
    //Apply gains
  Xos = (Xo / gainX);         // Calculating G Values for X, Y and Z  with offsets and gains applied
  Yos = (Yo / gainY);
  Zos = (Zo / gainZ);

  Serial.print("  Converted to G values: "); Serial.print(Xos); Serial.print("  "); Serial.print(Yos); Serial.print("  "); Serial.print(Zos);
  

  Modmove = sqrt((Xos*Xos)+(Yos*Yos)+(Zos*Zos));

  Serial.print("  Modvert  : "); Serial.print(Modvert); 
  Serial.print("  Modmove  : "); Serial.print(Modmove); 
 
  Costilt = (Xvert*Xos)+(Yvert*Yos)+(Zvert*Zos)/(Modvert*Modmove);
  
  Serial.print("  Costilt  : "); Serial.print(Costilt); 


  Tilt = acos(Costilt);

  Serial.print("  Tilt: "); Serial.print(Tilt); 
  Serial.println();

    
  delay(500);                          // This is the delay between each reading

 
    }

The accelerometer is at a tilt of about 11 degrees and the output looks like this

y z: -31 -15 239 Converted to G values: -0.17 -0.06 0.99 Modvert : 1.00 Modmove : 1.01 Costilt : 0.98 Tilt: 0.19
x y z: -32 -15 240 Converted to G values: -0.18 -0.06 0.99 Modvert : 1.00 Modmove : 1.01 Costilt : 0.98 Tilt: 0.19
x y z: -32 -15 239 Converted to G values: -0.18 -0.06 0.99 Modvert : 1.00 Modmove : 1.01 Costilt : 0.98 Tilt: 0.19

Thanks for any help.

Leroy
I suspect the angles on this one only go 0 to 90.

bms001
That wont work. If pitch was 44 and roll was 45 the reported angle would be 45 wheras the real value would be considerably greater