Hi there,
I am using Arduino MKR 1010 Wifi connected to an SD card and 400g accelerometer (Sparkfun H3LIS331DL Accelerometer) with SPI and I2C protocol, respectively. Technically, the maximum data rate of the accelerometer is 1000Hz, but I was only able to save data at 350Hz. I'm willing to improve my sampling rate to 1000Hz. If you can help me achieve this, it would be greatly appreciated. Here is the code:
#include <SPI.h>
#include <SD.h>
#include <WiFiNINA.h>
#include <ArduinoOTA.h>
#include <SDU.h>
#include "SparkFun_LIS331.h"
#include <Wire.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
float accel_z; //Collected accelerometer on z axis
const int chipselect = 4; //digital pin with SD card
int ax;
char buf[30];
int timeend = 299999999;
static long loopTimer = 0;
LIS331 xl;
int status = WL_IDLE_STATUS;
File AX1;
void setup() {
pinMode(9,INPUT); // Interrupt pin input
xl.setI2CAddr(0x19); // This MUST be called BEFORE .begin() so
// .begin() can communicate with the chip
Wire.begin();
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.
xl.setPowerMode(LIS331::NORMAL); //Normal power mode
xl.setODR(LIS331::DR_1000HZ); // data rate is considered equal to 1000HZ
xl.setFullScale(LIS331::HIGH_RANGE); //+-400g is the acceleration range considered for the H3LIS331DH accelerometer
// This next section configures an interrupt. It will cause pin
// INT1 on the accelerometer to go high when the absolute value
// of the reading on the Z-axis exceeds a certain level for a
// certain number of samples.
xl.intSrcConfig(LIS331::INT_SRC, 1); // Select the source of the
// signal which appears on pin INT1. In
// this case, we want the corresponding
// interrupt's status to appear.
xl.setIntDuration(50, 1); // Number of samples a value must meet
// the interrupt condition before an
// interrupt signal is issued. At the
// default rate of 50Hz, this is one sec.
xl.setIntThreshold(2, 1); // Threshold for an interrupt. This is
// not actual counts, but rather, actual
// counts divided by 16.
xl.enableInterrupt(LIS331::Z_AXIS, LIS331::TRIG_ON_HIGH, 1, false);
// Enable the interrupt. Parameters indicate
// which axis to sample, when to trigger
// (in this case, when the absolute mag
// of the signal exceeds the threshold),
// which interrupt source we're configuring,
// and whether to enable (true) or disable
// (false) the interrupt.
Serial.begin(115200);
// setup SD card
if (!SD.begin(SDCARD_SS_PIN)) {
// don't continue:
while (true);
}
//Initialize serial:
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
}
// start the WiFi OTA library with internal (flash) based storage
ArduinoOTA.begin(WiFi.localIP(), "Arduino", "pass", SDStorage);
// you're connected now
SD.remove("AX1.txt");
AX1 = SD.open("AX1.txt", FILE_WRITE);
AX1.print("Time"); AX1.print("\t"); AX1.print("az"); AX1.print("\n");
}
void loop()
{
ArduinoOTA.poll(); //check for WiFi OTA updates
int16_t x, y, z;
String dataString = "";
if (micros() - loopTimer > 10)
{
loopTimer = micros();
xl.readAxes(x, y, z); // The readAxes() function transfers the
// current axis readings into the three
// parameter variables passed to it.
dataString += String(accel_z = xl.convertToG(400,z));
AX1.print(micros());
AX1.print(" ");
AX1.println(dataString);
if ((loopTimer % 500) == 0) AX1.flush();
if (loopTimer > timeend) AX1.close();
}
}