Hi,
I am testing an Arduino Due with an Adafruit SD breakout (SPI), my aim is to datalog at 1000Hz.
I have a test sketch working, but the logging stops after a period.
- If I slow down the logging rate (increase milliseconds between samples), then the logging runs for longer, actually giving more entries in the file, even though the logging rate is lower.
-If I increase the number of characters written for each entry, then the logging stops sooner, fewer entries in the file.
Is there some overflow happening that I am not seeing, or is there something happening with SPI?
My test code:
#include <SD.h>
#include <Wire.h>
//Datalogging
unsigned int loggingRate = 1; // Milliseconds between samples
char buffer[600];
char buffer2[600];
unsigned long currentMillis = 0;
unsigned long nextMillis = 0;
// File object to represent file
File txtFile;
char fileName[13];
void setup()
{
Serial.begin(9600);
delay(2000);
// Initialise the SD card
if (!SD.begin()) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("SD card initialised");
delay(500);
newLogFile();
delay(3000);
}
void loop(){
currentMillis = millis();
if (currentMillis >= nextMillis) {
nextMillis = currentMillis + loggingRate;
readData();
logData();
}
}
void readData(){
char temp1[10];
char temp2[10];
strcpy(temp1, "123456789");
strcpy(temp2, "ABCDEFGHI");
sprintf(buffer + strlen(buffer), "%d %s %s\r\n", millis(), temp1, temp2);
}
void logData(){
unsigned int chunkSize = txtFile.availableForWrite();
if (chunkSize && strlen(buffer) >= chunkSize) {
// write to file
txtFile.write(buffer, chunkSize);
// Copy unwritten data to temporary array, clear main buffer, then copy unwritten data back to main array
memcpy(buffer2, buffer+(chunkSize), sizeof(buffer));
memset(buffer, 0, sizeof(buffer));
memcpy(buffer, buffer2, strlen(buffer2));
memset(buffer2, 0, sizeof(buffer2));
}
}
void newLogFile(){
strcpy(fileName, "test.txt");
txtFile = SD.open(fileName, FILE_WRITE);
if (!txtFile) {
Serial.print("error opening ");
Serial.println(fileName);
while (1);
}
}
This produces the following entries, as expected, in test.txt:
5619 123456789 ABCDEFGHI
5620 123456789 ABCDEFGHI
5621 123456789 ABCDEFGHI
and so on...
At 10ms logging interval it ran for about an hour before stopping. At 1ms logging interval it runs for a few seconds, sometimes a bit longer, sometimes shorter.
I am so close to getting this to work, just need to iron-out this last issue.
Can anybody please help?
Thank you!