I'm running simple loops using the Teensyduino iterating a variable (x) from 1 to, say, 10,000. It's a scaled down problem in an attempt to find a solution for a bug in a much larger code. The problem is that after writing a certain number of lines to the SD, it will stop writing for a certain quantity of values, then start back up again, e.g. In the most recent trial it recorded from 1 - 4293, then stopped, and began on the next line (Excel line 4294) with 14581, then continued until I cut it off around 15350.
Also, the odd time it will print two values on the same line instead of skipping to the next one.
I've been running the trials alongside Serial port recordings using the PuTTY program (very useful program) which creates a .txt or .csv file with the serial port output.
It's confusing, as in both the larger code and this simple one, the serial outputs are always exactly what is desired, and it's not a fault with the SD card as I've tried using a few different ones and the same problem emerges.
I'm thinking it might be a problem with opening and closing the datafile so often and quickly (would anyone have a better way of doing this?), or else maybe with the SD library.
Has anyone else had a similar problem?
Here's the code:
/*
Purpose of this program is to check for faults with writing to the SD card.
*/
#include <SD.h>
const int chipSelect = 5;
long int x = 0;
void setup()
{
Serial.begin(9600);
if (!SD.begin(chipSelect))
{
Serial.println("Card failed");
return;
}
Serial.println("SD card initialized.");
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
pinMode(5, OUTPUT);
digitalWrite(SS, HIGH);
digitalWrite(PIN_D7, HIGH);
toBegin();
}
void loop()
{
x = x + 1;
SD.begin(chipSelect);
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile)
{
dataFile.println(x);
dataFile.close();
Serial.println(x);
}
else
{
Serial.println("error opening datalog.csv");
}
delay(10);
}
void toBegin()
{
while (Serial.available() <= 0)
{
Serial.println('1', BYTE);
delay(300);
}
}
Any help would be greatly appreciated!
