Hi all,
I am currently trying to connect an accelerometer to an Arduino Uno via SPI communication. The only thing I want to achieve here is to read the z-axis acceleration and know precisely when these readings are taken relative to other readings. I will then print these values to the serial monitor.
Here is the current code I am using:
#include <SparkFun_ADXL345.h>
ADXL345 adxl = ADXL345(10); // set slave pin to 10
bool xPrint = false; // change these if I also want x or y data
bool yPrint = false; //
bool zPrint = true; //
void setup()
{
Serial.begin(9600); // set baud rate at 9600
adxl.powerOn(); // turn on accelerometer
adxl.setRangeSetting(4); // set range to +/- 4g
adxl.setSpiBit(0); // initialize 4-wire connection
adxl.set_bw(3200); // bandwidth should be 0.1Hz to 3200Hz (according to datasheet)
adxl.setRate(6400); // data rate should be twice the bandwidth (according to datasheet)
}
void loop()
{
int x,y,z;
adxl.readAccel(&x, &y, &z); // read data from accelerometer
xPrint ? Serial.print(x) : Serial.print(""); // if x-data is enabled, print its value,
xPrint ? Serial.print(' ') : Serial.print(""); // otherwise don't print anything
yPrint ? Serial.print(y) : Serial.print("");
yPrint ? Serial.print(' ') : Serial.print("");
zPrint ? Serial.print(z) : Serial.print("");
zPrint ? Serial.print(' ') : Serial.print("");
Serial.println(millis()/1000.0,3); // print a timestamp on the same line
}
My main concern is the timing. My questions are:
- Should the baud rate of the serial monitor correlate to the accelerometer's bandwidth or data rate at all?
- Does setting the bandwidth to 3200Hz ensure the data readings are being taken at that rate?
- Above all, how can I print the accelerometer's data to the serial monitor at precise intervals?
I am also considering not printing a time stamp and just assuming that the readings are being taken at that set rate. I could still make the time domain graph this way.
Any advice is greatly appreciated. Thanks!