ADXL345 + arduino how to call library

Hello, I am a beginner for using arduino library. I am doing a project to programme ADXL345 through using arduino.

Now that I have connect ADXL345 to arduino through I2C like the link here. http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html

I would like to set the range, do the offset for different axis through using the self-test function of the ADXL345.

I also find the ADXL345 library from a website. http://bildr.org/2011/03/adxl345-arduino/

In the library, I have found those programme in the file Adxl345.cpp

For range setting:

// Gets the range setting and return it into rangeSetting
// it can be 2, 4, 8 or 16
void ADXL345::getRangeSetting(byte* rangeSetting) {
    byte _b;
    readFrom(ADXL345_DATA_FORMAT, 1, &_b);
    *rangeSetting = _b & B00000011;
}

// Sets the range setting, possible values are: 2, 4, 8, 16
void ADXL345::setRangeSetting(int val) {
    byte _s;
    byte _b;

    switch (val) {
        case 2:  
            _s = B00000000; 
            break;
        case 4:  
            _s = B00000001; 
            break;
        case 8:  
            _s = B00000010; 
            break;
        case 16: 
            _s = B00000011; 
            break;
        default: 
            _s = B00000000;
    }
    readFrom(ADXL345_DATA_FORMAT, 1, &_b);
    _s |= (_b & B11101100);
    writeTo(ADXL345_DATA_FORMAT, _s);
}

For Self-test:

// gets the state of the SELF_TEST bit
bool ADXL345::getSelfTestBit() {
    return getRegisterBit(ADXL345_DATA_FORMAT, 7);
}

// Sets the SELF-TEST bit
// if set to 1 it applies a self-test force to the sensor causing a shift in the output data
// if set to 0 it disables the self-test force
void ADXL345::setSelfTestBit(bool selfTestBit) {
    setRegisterBit(ADXL345_DATA_FORMAT, 7, selfTestBit);
}

For offset:

// Sets the OFSX, OFSY and OFSZ bytes
// OFSX, OFSY and OFSZ are user offset adjustments in twos complement format with
// a scale factor of 15,6mg/LSB
// OFSX, OFSY and OFSZ should be comprised between 
void ADXL345::setAxisOffset(int x, int y, int z) {
    writeTo(ADXL345_OFSX, byte (x));  
    writeTo(ADXL345_OFSY, byte (y));  
    writeTo(ADXL345_OFSZ, byte (z));  
}

// Gets the OFSX, OFSY and OFSZ bytes
void ADXL345::getAxisOffset(int* x, int* y, int*z) {
    byte _b;
    readFrom(ADXL345_OFSX, 1, &_b);  
    *x = int (_b);
    readFrom(ADXL345_OFSY, 1, &_b);  
    *y = int (_b);
    readFrom(ADXL345_OFSZ, 1, &_b);  
    *z = int (_b);
}

Now that I can not call those funcion from the library. I would like to ask how to call the library in the Arduino programme. Do anyone have this ADXL345 programming experience?? Thank You=]

Now that I can not call those funcion from the library.

Why not?

Thanks for replying.

I know that arduino programme divide into two part: void stepup and void loop. For this library, can I just simply add "adxl.setSelfTestBit()" inside void loop for using self-test function?? Thank You.

You can add a call to adxl.setSelfTestBit(), after you create the object adxl. Whether that should be done in setup() or loop() depends on whether that function needs to be called once, or more than once.

I see. I am going to set the range of the ADXL345 accelerometer. the function in the library is

// Gets the range setting and return it into rangeSetting
// it can be 2, 4, 8 or 16
void ADXL345::getRangeSetting(byte* rangeSetting) {
  byte _b;
  readFrom(ADXL345_DATA_FORMAT, 1, &_b);
  *rangeSetting = _b & B00000011;
}

// Sets the range setting, possible values are: 2, 4, 8, 16
void ADXL345::setRangeSetting(int val) {
  byte _s;
  byte _b;

  switch (val) {
  case 2:  
    _s = B00000000; 
    break;
  case 4:  
    _s = B00000001; 
    break;
  case 8:  
    _s = B00000010; 
    break;
  case 16: 
    _s = B00000011; 
    break;
  default: 
    _s = B00000000;
  }
  readFrom(ADXL345_DATA_FORMAT, 1, &_b);
  _s |= (_b & B11101100);
  writeTo(ADXL345_DATA_FORMAT, _s);
}

In arduino program, I write a code

 accel.setRangeSetting(8);
  
  Serial.print("getRangeSetting():");
  Serial.println(accel.getRangeSetting(B1000), DEC);

but it can't compilie. I would like to ask what byte do I need to write(inside accel.getRangeSetting(?????)) for setting the range of 8 and show that I set the right range. Thank You=]

The declaration tells you (well, me at least) that the function is going to return a value through the argument. So, the argument must be a variable that the function can write to. B1000 is not a variable.

Thanks. The code are now corrected to following:

accel.setRangeSetting(16);
  Serial.print("getRangeSetting(): ");
  byte* temp;
  accel.getRangeSetting(temp);
  Serial.println(*temp, DEC);

and I can get the result. The result is "getRangeSetting(): 3" Do i really set the range of 16 of the accelerometer??

void ADXL345::getRangeSetting(byte* rangeSetting) {
  byte _b;
  readFrom(ADXL345_DATA_FORMAT, 1, &_b);
  *rangeSetting = _b & B00000011;
}

Why not simply:

void ADXL345::getRangeSetting(byte* rangeSetting) {

  readFrom(ADXL345_DATA_FORMAT, 1,  rangeSetting);
  *rangeSetting &= _b B00000011;
}

?

got it! Thanks!
I am now using the selftest and the offset function to calibrate the accelerometer.
How do I do that??

I plan to combine the code of selftest

// gets the state of the SELF_TEST bit
bool ADXL345::getSelfTestBit() {
  return getRegisterBit(ADXL345_DATA_FORMAT, 7);
}

// Sets the SELF-TEST bit
// if set to 1 it applies a self-test force to the sensor causing a shift in the output data
// if set to 0 it disables the self-test force
void ADXL345::setSelfTestBit(bool selfTestBit) {
  setRegisterBit(ADXL345_DATA_FORMAT, 7, selfTestBit);
}

and the code of readAccel:

// Reads the acceleration into three variable x, y and z
void ADXL345::readAccel(int *xyz){
  readAccel(xyz, xyz + 1, xyz + 2);
}
void ADXL345::readAccel(int *x, int *y, int *z) {
  readFrom(ADXL345_DATAX0, TO_READ, _buff); //read the acceleration data from the ADXL345

  // each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  // thus we are converting both bytes in to one int
  *x = (((int)_buff[1]) << 8) | _buff[0];   
  *y = (((int)_buff[3]) << 8) | _buff[2];
  *z = (((int)_buff[5]) << 8) | _buff[4];
}

Anyone have this experience??
Thank You!