Hello! I am trying to adjust the sample rate and range on my H3LIS331DL but I am having difficulty implementing the code provided on the accelerometer’s info page into my code correctly. I am running it using I2C with an Arduino Pro Mini and a RTC also using I2C on the same wires. I would like to use the 200g setting with as high of a sample rate as possible, so hopefully the 1000Hz that is advertised on the products spec sheet. I have tried putting the “LIS331::DR_1000HZ” line of code in the setup and loop sections of the code separately but it never seems to have an effect on the sample rate. If anyone has any experience with this issue, your help would be greatly appreciated.
Here is my code:
(Edited by moderator to include code tags.)
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
RTC_PCF8523 rtc;
#include "SparkFun_LIS331.h"
#include <Wire.h>
LIS331 xl;
File myFile;
//const int delaytime = 5; // Time between readings, counted as milliseconds
float scaledReadings [3][3]; //Storage for the data collected by the accelerometer
const int button = 2; // pushbutton pin
int buttonState; // for reading the pushbutton state
int state = 0; //For deciding if the system is starting or going into another loop
void setup() {
Wire.begin();
xl.setI2CAddr(0x19); // This MUST be called BEFORE .begin() so
// .begin() can communicate with the chip
xl.begin(LIS331::USE_I2C); // Selects the bus to be used and sets
// the power up bit on the accelerometer.
// Also zeroes out all accelerometer
// registers that are user writable.
// Initialize serial communication at 115200 baud
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}
// Initialized the pushbutton as input
pinMode(button, INPUT_PULLUP);
//initialize Real Time Clock (RTC)
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if(! rtc.initialized() || rtc.lostPower()) {
Serial.println("RTC is NOT initialized, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// see if the card is present
if (!SD.begin(10)) {
Serial.println("Card init. failed!");
}
Serial.println("Starting data collection.");
}
void loop()
{
buttonState = digitalRead(button);
if(buttonState == 0 && state == 0){
//Create a file name for the data to be stored
char filename[15];
strcpy(filename, "DATA00.CSV");
for (uint8_t i = 0; i < 100; i++) {
filename[4] = '0' + i / 10;
filename[5] = '0' + i % 10;
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename)) {
break;
}
}
myFile = SD.open(filename, FILE_WRITE);
if ( ! myFile ) {
Serial.print("Couldnt create ");
Serial.println(filename);
}
//get the last filename
strcpy(filename, "DATA00.CSV");
for (uint8_t i = 0; i < 100; i++) {
filename[4] = '0' + i / 10;
filename[5] = '0' + i % 10;
if (!SD.exists(filename)) {
filename[4] = '0' + (i - 1) / 10;
filename[5] = '0' + (i - 1) % 10;
break;
}
}
//Initialize SD card and make sure it has opened correctly
Serial.println("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("successful!");
myFile = SD.open(filename, FILE_WRITE);
delay(1000);
}
if (myFile) {
Serial.println("Your file has opened, beginning to record data.");
//Take readings from the accelerometer
if (buttonState == 0) {
do {
readWrite();
buttonState = digitalRead(button);
} while (buttonState == 0);
state = 1;
}
if (buttonState == 1 && state == 1){
myFile.close();
Serial.println("Data Saved");
state = 0;
}
}
else {
Serial.println("Waiting for system to be turned on");
}
}
void readWrite() {
int16_t x, y, z;
String dataString = "";
xl.readAxes(x, y, z); // The readAxes() function transfers the
// current axis readings into the three
// parameter variables passed to it.
DateTime now = rtc.now();
dataString += String(now.hour());
dataString += ":";
dataString += String(now.minute());
dataString += ":";
dataString += String(now.second());
dataString += ",";
scaledReadings[0][0] = xl.convertToG(200,x);
dataString += String(scaledReadings[0][0]);
dataString += ",";
scaledReadings[1][0] = xl.convertToG(200,y);
dataString += String(scaledReadings[1][0]);
dataString += ",";
scaledReadings[2][0] = xl.convertToG(200,z);
dataString += String(scaledReadings[2][0]);
//For debugging of the sensor readings
Serial.println(dataString);
myFile.println(dataString);
//delay(delaytime);
}
// Same functionality as Arduino's standard map function, except using floats
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}