BriComp
Well-known member
I have a system where I gather data and write it to a file every minute.
If I cut the file (from Teensy) and paste elsewhere on the PC the Teensy will not re-create the file and write to it.
In fact any operation on the Teensy file by the PC STOPS Teensy from writing to the file WITH NO ERROR THROWN.
Below is a program which demonstrates the problem.
If I cut the file (from Teensy) and paste elsewhere on the PC the Teensy will not re-create the file and write to it.
dataFile = SD.open("datalogZ.csv", FILE_WRITE);
is supposed to open a file if it exists or create it if it does not.In fact any operation on the Teensy file by the PC STOPS Teensy from writing to the file WITH NO ERROR THROWN.
Below is a program which demonstrates the problem.
Code:
/*
Demonstrate MTP/SD Problem
*/
#define softwareId "S.0004.0004.003 - Added MTP support"
#include <SD.h>
#include <timelib.h>
#include <MTP_Teensy.h>
#define CS_SD BUILTIN_SDCARD // Works on T_3.6 and T_4.1
#define ledPin 13
File dataFile;
String GenerateDateStr(char c) {
String s = "";
int n = day();
if (n < 10) s.concat( "0");
s.concat(n);
s.concat('/');
n = month();
if (n < 10) s.concat("0");
s.concat(n);
s.concat('/');
n = year();
if (n < 10) s.concat("0");
s.concat(n);
if (c != 0x0) s.concat(c);
return s;
}
String GenerateTimeStr(bool andSeconds, char c ) {
String s = "";
int n = hour();
if (n < 10) s.concat("0");
s.concat(n);
s.concat(':');
n = minute();
if (n < 10) s.concat("0");
s.concat(n);
if (andSeconds) {
s.concat(':');
n = second();
if (n < 10) s.concat("0");
s.concat(n);
}
if (c != 0x0) s.concat(c);
return s;
}
time_t getTeensy3Time(){
return Teensy3Clock.get();
}
bool sdSetup = true;
void setup()
{
setSyncProvider(getTeensy3Time);
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial1.begin(115200);
Serial.print("Initializing SD card...");
pinMode(ledPin, OUTPUT);
// see if the card is present and can be initialized:
if (SD.begin(CS_SD)) {
MTP.addFilesystem(SD, "SD Card");
Serial.println("card initialized.");
}
else {
Serial.println("Card failed, or not present");
sdSetup = false;
// No SD card, so don't do anything more - stay stuck here
}
if (timeStatus() != timeSet) {
Serial.println("Unable to sync with the RTC");
} else Serial.println("RTC has set the system time");
// mandatory to begin the MTP session.
MTP.begin();
Serial.println(GenerateDateStr(0x0));
}
void PrintNN(uint16_t n) {
uint16_t q;
dataFile.print(n / 100);
dataFile.print(".");
q = n % 100;
if (q < 10) dataFile.print("0");
dataFile.print(q);
}
String tim;
String date;
uint8_t lastMin = 99;
uint8_t currMin = 98;
uint8_t p;
// Add the main program code into the continuous loop() function
void loop()
{
MTP.loop(); //This is mandatory to be placed in the loop code.
currMin = minute();
while (lastMin != currMin) {
digitalWrite(ledPin, HIGH);
if (currMin != lastMin) {
if (lastMin != 99) {
Serial.print("sdSetup:"); Serial.print(sdSetup); Serial.print(" SD.mediaPresent:"); Serial.print(SD.mediaPresent());
if (sdSetup && SD.mediaPresent()) {
dataFile = SD.open("datalogZ.csv", FILE_WRITE);
Serial.print(" dataFile:"); Serial.println(dataFile);
// if the file is available, write to it:
if (dataFile) {
Serial.println("Writing to file");
date = GenerateDateStr(' ');
tim = GenerateTimeStr(true, 0x0);
dataFile.print(date);
dataFile.println(tim);
dataFile.close();
}
}
}
lastMin = currMin;
}
// Serial.print("currMin: "); Serial.print(currMin); Serial.print(" lastMin: "); Serial.println(lastMin);
// delay(1000);
digitalWrite(ledPin, LOW);
}
delay(1);
}