qwazzerman
Member
I'm making a datalogging program using Teensy 3.1 and an I2C sensor, with the SD card reader on the ILI9341.
I'm using vectors to store the data, since the logging duration is variable. The sampling rate is ~100Hz (every 10 ms), so rather than writing the data to a file continuously, I'm waiting until the end and writing everything all at once.
What's the best way to write these vectors to a file on an SD card? They'll contain up to 3000 values (in the most extreme case).
I've found two options that seem reasonable, but I'm a novice at C++, and I'm unsure if one is better suited to the Teensy environment.
Option 1:
(It doesn't use the SD library yet, since I'm waiting for my Teensy to arrive, but I'm hoping that making it work with the SD library is easy)
Which gives me
Option 2:
Adapted from the Datalogger example in the SD library
Or, would it be better to figure out how to write the data continuously as it's being collected?
I'm using vectors to store the data, since the logging duration is variable. The sampling rate is ~100Hz (every 10 ms), so rather than writing the data to a file continuously, I'm waiting until the end and writing everything all at once.
What's the best way to write these vectors to a file on an SD card? They'll contain up to 3000 values (in the most extreme case).
I've found two options that seem reasonable, but I'm a novice at C++, and I'm unsure if one is better suited to the Teensy environment.
Option 1:
(It doesn't use the SD library yet, since I'm waiting for my Teensy to arrive, but I'm hoping that making it work with the SD library is easy)
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<double> foo;
for (int i = 1; i < 100; i++) //Build arbitrary vector
{
foo.push_back(i);
}
ofstream outputFile("output.txt");
outputFile << "Trial 1\t";
copy(foo.begin(), foo.end(), ostream_iterator<double>(outputFile, "\t"));
outputFile << endl;
}
Code:
Trial 1 1 2 3 4 5 6 7 8 9 10 11 12...
Option 2:
Adapted from the Datalogger example in the SD library
Code:
#include <SD.h>
#include <SPI.h>
#include <vector>
int chipSelect = 21;
int main()
{
std::vector<double> foo;
for (int i = 1; i < 100; i++) //Build arbitrary vector
{
foo.push_back(i);
}
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print("Trial 1");
for (int i = 1; foo.size()-1; i++)
{
dataFile.print("\t");
dataFile.print(foo[i]);
}
dataFile.close();
}
}
Or, would it be better to figure out how to write the data continuously as it's being collected?