Hi,
I've been running an accelerometer and datalogger for a few months now and I've noticed some strange behavior with regards to the data that has been written to the SD card.
My write speed starts off at around 100Hz but slowly as more data is written to the file the write speed ends up at around 20Hz after maybe 5-8hrs of data is recorded.
Now I can guess why this might be happening? Possibly something to do with it having to open find the last line in the txt/csv file, write data, close, repeat. Each time this takes a little longer as there is a new line of text.....maybe?
Can anybody help suggest as way to fix this please?
I supposed I could ask it to write a new file each time the ardunio is powered up, but that is a bit of a work around and not really addressing the issue.
Thanks in advance
I'm using a Nano, with adafruit micro SD module and LIS3DH acceleromter (+ RTC)
#include <SD.h> //SD Card Module
#include <DS3231.h> //RTC
#include <Adafruit_LIS3DH.h> //Accelerometer
#include <dht.h> //Temp Sensor
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
//Temp Sensor
#define dataPin 4 // Defines pin number to which the sensor is connected
dht DHT;
//SD Card Module
File sdcard_file;
int CS_pin = 10; // Pin 10 on Arduino Uno
//RTC
DS3231 rtc(SDA, SCL);
//Accelerometer Module
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
//Millis() function
#define Accelerometer_Intival 20 //Accelerometer reading intival in milliseconds
#define Tempertaure_Intival 60000 //Temperature reading intival in milliseconds
unsigned long currentMillis;
unsigned long Acc_Time;
unsigned long Temp_Time;
void setup() {
Serial.begin(115200);
Acc_Time = millis(); //initial start time
Temp_Time = millis(); //initial start time
rtc.begin(); //Start RCT
lis.begin(); //Start Accelerometer
lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
//Temp Sensor Setup
int readData = DHT.read22(dataPin); // DHT22/AM2302
float temp = DHT.temperature; // Gets the values of the temperature
float humid = DHT.humidity; // Gets the values of the humidity
// SD Card Initialization - called once on initial power up
if (SD.begin())
sdcard_file = SD.open("data1.txt", FILE_WRITE);
if (sdcard_file) {
sdcard_file.println("### NEW EVENT ###"); //Denotes new event
sdcard_file.close(); // close the file
}
}
void loop() {
currentMillis = millis();
accel();
temp();
}
void accel() //Read accelerometer and write to SD card
{
if(currentMillis > Acc_Time + Accelerometer_Intival)
{
//Accelerometer Read (normalised m/s^2)
sensors_event_t event;
lis.getEvent(&event);
//Write to SD Card
sdcard_file = SD.open("data1.txt", FILE_WRITE);
if (sdcard_file) {
sdcard_file.print(rtc.getDateStr());
sdcard_file.print("\t");
sdcard_file.print(rtc.getTimeStr());
sdcard_file.print("\t");
sdcard_file.print(event.acceleration.x);
sdcard_file.print("\t");
sdcard_file.print(event.acceleration.y);
sdcard_file.print("\t");
sdcard_file.println(event.acceleration.z);
sdcard_file.close(); // close the file
Acc_Time = currentMillis;
}
}
}
void temp() //Read Teamp and Humidity and wirte to SD card
{
if(currentMillis > Temp_Time + Tempertaure_Intival)
{
//Temp Read
int readData = DHT.read22(dataPin); // DHT22/AM2302
float temp = DHT.temperature; // Gets the values of the temperature
float humid = DHT.humidity; // Gets the values of the humidity
//Write to SD Card
sdcard_file = SD.open("data1.txt", FILE_WRITE);
if (sdcard_file) {
sdcard_file.print(rtc.getDateStr());
sdcard_file.print("\t");
sdcard_file.print(rtc.getTimeStr());
sdcard_file.print("\t");
sdcard_file.print("\t");
sdcard_file.print("\t");
sdcard_file.print("\t");
sdcard_file.print((float)humid, 2);
sdcard_file.print("\t");
sdcard_file.println((float)temp, 2);
sdcard_file.close(); // close the file
}
Temp_Time = currentMillis;
}
}