#include #define lim 512 #define DEVICE 0x53 //ADXL345 device address #define TO_READ 6 //num of bytes we are going to read each time (two bytes for each axis) int lecturas = 1; byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device char str[512]; //string buffer to transform data before writeing it to the serial port int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345 long int x=0; long int y=0; long int z=0; int x_old=0; int y_old=0; int z_old=0; uint32_t lastSend = 0; void setup() { Serial.begin(9600); Wire.begin(); // join i2c bus (address optional for master) writeTo(DEVICE, 0x2D, 0x08); // writeTo(DEVICE, 0x31, 0x00); //2g // writeTo(DEVICE, 0x31, 0x01); //4g writeTo(DEVICE, 0x31, 0x02); //8g // writeTo(DEVICE, 0x31, 0x03); //16g delay(15); } void loop() { checkAdxl(); Serial.print("x= "); Serial.print(x); Serial.print(", "); Serial.print("y= "); Serial.print(y); Serial.print(", "); Serial.print("z= "); Serial.println(z); delay(100); } void checkAdxl() { byte res = 0; averageADXL(); if ( (x > (x_old + res) ) || (x < (x_old - res) ) ) { x_old = x; } if ( (y > (y_old + res) ) || (y < (y_old - res) ) ) { y_old = y; } if ( (z > (z_old + res) ) || (z < (z_old - res) ) ) { z_old = z; } } int average(int anaPin) { long total = 0; long average = 0; int count = 0; for(int i=0; i