Hi All,
I am using Arduino Uno R3 to communicate with ADXL345 accelerometer. Ideally I am trying to built fall detection system BUT at the moment I am stuck to get basic correct communication from sensor. I am not getting right values for x, y and z axis for +-2g, +-4g, +-8g and +-16g. I am getting readings from accelerometer and they also change when I move the board but they are not according to the SENSITIVITY values given in ADXL345 datasheet (page 4 of 40) for each g. Please see following code I picked up from sparkfun. I think my problem is to add two signed bytes. Please see my code as follow.. I would appreciate help from you all
#include <SPI.h>
#define PINNUMBER
int CS=10;
char values[10];
int x,y,z;
double xg, yg, zg;
char ff=0;
long line = 0;
#define DEVID 0x00 //Device ID Register
#define POWER_CTL 0x2D //Power Control Register
#define DATA_FORMAT 0x31 //Data format control
#define DATAX0 0x32 //X-Axis Data 0
#define DATAX1 0x33 //X-Axis Data 1
#define DATAY0 0x34 //Y-Axis Data 0
#define DATAY1 0x35 //Y-Axis Data 1
#define DATAZ0 0x36 //Z-Axis Data 0
#define DATAZ1 0x37 //Z-Axis Data 1
void setup(){
SPI.begin();
SPI.setDataMode(SPI_MODE3); //configure accelerometer for SPI connecttion
Serial.begin(9600);
pinMode(CS, OUTPUT); //set chip select to be output
digitalWrite(CS, HIGH); //set chip select to be high
writeRegister(DATA_FORMAT, 0x01); //put accelerometer into 16G range
writeRegister(POWER_CTL, 0x08); //Measurement mode
}
void loop(){
readRegister(DATAX0, 6, values);
int i;
x = ((int)values[1]<<8)|(int)values[0];
y = ((int)values[3]<<8)|(int)values[2]; //I think my problem is in these three lines.. calculations to add two signed bytes.
z = ((int)values[5]<<8)|(int)values[4];
// x = values[1] << 8;
// x+= values[0];
Serial.println();
for (i=0; i<6; i++)
{
Serial.print(values[i], DEC);
Serial.print(" ");
}
Serial.println();
Serial.print("X: ");
Serial.print(x, DEC);
Serial.print(',');
Serial.print("Y: ");
Serial.print(y, DEC);
Serial.print(',');
Serial.print("Z: ");
Serial.print(z, DEC);
Serial.print(',');
delay(1000);
//convert accelerometer value to G
// xg = x * 0.0039;
// yg = y * 0.0039;
// zg = z * 0.0039;
//Print the results to the terminal so that i can monitor the reading using the serial monitor.
// Serial.println ();
// Serial.print("xg: ");
// Serial.print((float)xg,2);
// Serial.print("g,");
// Serial.print("yg: ");
// Serial.print((float)yg,2);
// Serial.print("g,");
// Serial.print("zg: ");
// Serial.print((float)zg,2);
// Serial.println("g");
delay(1500);
}
// char registerAddress - The register to write a value to
// char value - The value to be written to the specified register.
void writeRegister(char registerAddress, char value){
//Set Chip Select pin low to signal the beginning of an SPI packet.
digitalWrite(CS, LOW); // to signal beginning of SPI packet
SPI.transfer(registerAddress);
SPI.transfer(value);
digitalWrite(CS, HIGH); // to signal end of SPI packet
}
void readRegister(char registerAddress, int numBytes, char * values){
char address = 0x80 | registerAddress; // to perform read operation the most significant bit of the register address must be set
if(numBytes > 1)address = address | 0x40;
digitalWrite(CS, LOW);
SPI.transfer(address);
for(int i=0; i<numBytes; i++){
values [i] = SPI.transfer(0x00);
}
digitalWrite(CS, HIGH);
}
Thank you for help.
Kind Regards