Hi,
I am reading data from an accelerometer (all X, Y and Z) values at a rate of 100 Hz.
I need to log this data to an SD card. So my initial code to do that was simple. Just grab the acceleration data and keep it in a string with the data delimited by a comma:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM9DS0.h>
#include <Adafruit_Sensor.h>
#include <SD.h>
String dataString;
unsigned long timestamp;
File logfile;
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0();
void setupSensor()
{
lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
}
void setup()
{
Serial.begin(9600);
Serial.println("LSM raw read demo");
// Try to initialise and warn if we couldn't detect the chip
if (!lsm.begin())
{
Serial.println("Oops ... unable to initialize the LSM9DS0. Check your wiring!");
while (1);
}
Serial.println("Found LSM9DS0 9DOF");
Serial.println("");
Serial.println("");
if (!SD.begin(4))
{
Serial.println("No SD card detected. Data will be transmitted, but not stored."); // No card available? Let us know.
}
else
logfile = SD.open("Data.txt", FILE_WRITE);
}
void loop()
{
sensors_event_t accel;
lsm.readAccel();
uint32_t timestamp = micros();
lsm.getAccelEvent(&accel,timestamp);
dataString =
timestamp + String(",")
+ accel.acceleration.x + String(",")
+ accel.acceleration.y + String(",")
+ accel.acceleration.z + String(",");
logfile.println(dataString);
logfile.flush();
}
So I have a wireless SD shield, put the SD card in and run the code. However writing to the SD card is extremely slow this way. I took an average of all intervals and that came out to be approximately 40 Hz. I have included this file as an attachment as LS.txt to show you what it looks like. But my rate is being halved or more. I figured this is because storing all the data in a string is extremely slow and I read some threads here and thread and decided to use a struct, like so:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM9DS0.h>
#include <Adafruit_Sensor.h>
#include <SD.h>
String dataString;
unsigned long timestamp;
File logfile;
struct datastore {
unsigned long ts;
int16_t ax;
int16_t ay;
int16_t az;
};
struct datastore myData;
byte *buff = (byte*)&myData;
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0();
void setupSensor()
{
lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
}
void setup()
{
Serial.begin(9600);
Serial.println("LSM raw read demo");
// Try to initialise and warn if we couldn't detect the chip
if (!lsm.begin())
{
Serial.println("Oops ... unable to initialize the LSM9DS0. Check your wiring!");
while (1);
}
Serial.println("Found LSM9DS0 9DOF");
Serial.println("");
Serial.println("");
if (!SD.begin(4))
{
Serial.println("No SD card detected. Data will be transmitted, but not stored."); // No card available? Let us know.
}
else
logfile = SD.open("clc.txt", FILE_WRITE);
}
void loop()
{
sensors_event_t accel;
lsm.readAccel();
uint32_t timestamp = micros();
lsm.getAccelEvent(&accel,timestamp);
for (int i = 0; i < 500; i++)
{
myData.ts = micros();
myData.ax = accel.acceleration.x;
myData.ay = accel.acceleration.y;
myData.az = accel.acceleration.z;
logfile.write((byte*)&myData, sizeof(datastore));
}
logfile.close();
}
The problem is, while it appears to be working (file is saved on the SD card and there is some data inside it), the notepad file is saved with what I assume is garbled data. I've attached this file as clc.txt. I think this is because it just saves byte data and not a string and notepad can't read it. I want this to be readable just like the file with the saved string data (first example).
So my questions are: 1) is this approach of using a struct correct to efficiently save the data? and 2) if so, how can I get readable data on my file. if not, how should I go about saving these "packets" of data every 100 Hz?
Regards,
CLC.TXT (9.77 KB)
LS.TXT (14.8 KB)