#include <Wire.h>
#define DEVICE1 (0x53) // Device address as specified in data sheet (SDO set to GND)
#define DEVICE2 (0x1D) // Second Device address as specified in data sheet (SDO set to +3.3V)
byte _buff[6];
char POWER_CTL = 0x2D; //Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1
int x1, y1, z1; // variables to hold final data for accel 1
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(19200); // start serial for output. Make sure you set your Serial Monitor to the same!
Serial.println("init");
//Put the ADXL345 1 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
newWriteTo(DATA_FORMAT, 0x01, DEVICE1);
//Put the ADXL345 1 into Measurement Mode by writing 0x08 to the POWER_CTL register.
newWriteTo(POWER_CTL, 0x08, DEVICE1);
//Put the ADXL345 1 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
newWriteTo(DATA_FORMAT, 0x01, DEVICE2);
//Put the ADXL345 1 into Measurement Mode by writing 0x08 to the POWER_CTL register.
newWriteTo(POWER_CTL, 0x08, DEVICE2);
}
void loop()
{
newReadAccel(); // read the x/y/z tilt on accelerometer
delay(50); // read every 50 ms only
}
// function to read accelerometer data from chosen device using Wire library and send them to serial port
void newReadAccel() {
uint8_t howManyBytesToRead = 6;
newReadFrom(DATAX0, howManyBytesToRead, _buff, DEVICE1); //function call to 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
x1 = (((int)_buff[1]) << 8) | _buff[0];
y1 = (((int)_buff[3]) << 8) | _buff[2];
z1 = (((int)_buff[5]) << 8) | _buff[4];
Serial.print("x1: ");
Serial.print( x1 );
Serial.print(" y1: ");
Serial.print( y1 );
Serial.print(" z1: ");
Serial.print( z1 );
delay(2);
}
// newWriteTo function to write data into I2C devices with chosen device address
void newWriteTo(byte address, byte val, char device) {
Wire.beginTransmission(device); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
// newReadFrom Function to read num bytes starting from address register on chosen device in to _buff array
void newReadFrom(byte address, int num, byte _buff[], char mydevice) {
Wire.beginTransmission(mydevice); // start transmission to device
Wire.write(address); // sends address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(mydevice); // start transmission to device
Wire.requestFrom(mydevice, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) // device may send less than requested (abnormal)
{
_buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); // end transmission
}
// low-pass (smooth) function
int smooth(int data, float filterVal, float smoothedVal){
if (filterVal > 1){ // check to make sure param's are within range
filterVal = .99;
}
else if (filterVal <= 0){
filterVal = 0;
}
smoothedVal = (data * (1 - filterVal)) + (smoothedVal * filterVal);
return (int)smoothedVal;
}