Hello everyone. I’m using the Sparkfun code for ADXL345 and I want to measure acceleration values with a NodeMcu and ADXL345 and e.g. perform an effective value calculation over 1 sec using the formula a_hvx = sqrt ((1 / N) * sum (x ^ 2)). SPI is used here as a communication protocol with a sampling rate of 1600 Hz.
My problem is that I get more values per second. I should normally get 1600 values per second. I programmatically set the bandwidth to 800 Hz (see code below).
#include <SparkFun_ADXL345.h> // SparkFun ADXL345 Library
/*********** COMMUNICATION SELECTION ***********/
/* Comment Out The One You Are Not Using */
ADXL345 adxl = ADXL345(5); // USE FOR SPI COMMUNICATION, ADXL345(CS_PIN);
void setup(){
Serial.begin(9600); // Start the serial terminal
Serial.println(“SparkFun ADXL345 Accelerometer Hook Up Guide Example”);
Serial.println();
adxl.powerOn(); // Power on the ADXL345
adxl.setRangeSetting(16); // Give the range settings
// Accepted values are 2g, 4g, 8g or 16g
// Higher Values = Wider Measurement Range
// Lower Values = Greater Sensitivity
adxl.set_bw(ADXL345_BW_800); // set the Band width
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
}
void loop(){
// Accelerometer Readings
int x,y,z;
int t, f_rate,i;
int xsum= 0.0, ysum= 0.0, zsum= 0.0;
float a_hvx, a_hvy, a_hvz, cons, N;
f_rate= 1600 ; // Data rate
t= 1 ; // measurement time
N= t*f_rate; // calculate the samples during current time (t)
// int arr[N+1];
cons= 1/N;
for (i=0; i< N; i++)
{
adxl.readAccel(&x,&y,&z); // Read the accelerometer values and store them in variables declared above x,y,z
xsum += pow(x,2);
ysum += pow(y,2);
zsum += pow(z,2);
}
Serial.print("xsum = ");
Serial.print(xsum);
Serial.print(", ");
Serial.print("ysum = ");
Serial.print(ysum);
Serial.print(", ");
Serial.print("zsum = ");
Serial.print(zsum);
Serial.print(", ");
a_hvx= sqrt(cons*xsum);
a_hvy= sqrt(cons*ysum);
a_hvz= sqrt(cons*zsum);
Serial.print("a_hvx = ");
Serial.print(a_hvx);
Serial.print(", ");
Serial.print("a_hvy = ");
Serial.print(a_hvy);
Serial.print(", ");
Serial.print("a_hvz = ");
Serial.println(a_hvz);
}