Hi All,
First time posting on here, just reached my boiling point and need some assistance. I'm using a Teensy 3.6, with built in SD card, trying to create a filename that simply appends the datestamp to the filename, and appends also ".csv". As I'm looking to create a file titled "DataLog20200318.csv" and of course based on a trigger develop a few different data log file with unique timestamps. My code works fine when providing
However, getting so extremely fancy as to...
If I simply substitue SD.open("datalog.csv", FILE_WRITE); ...with this... SD.open(filename, FILE_WRITE); it compiles but does not ever create nor find my file. I even tried \" thinking the SD library needed quotes around it as a complete character string to parse into the characters.
I guess I don't understand the road block I'm running into... Am I overlooking some simply character array size or what is it?
Here is the demo code I tried from the Data Logging demo that works until I change from a static text to the filename variable:
Thanks, your frustrated programmer
First time posting on here, just reached my boiling point and need some assistance. I'm using a Teensy 3.6, with built in SD card, trying to create a filename that simply appends the datestamp to the filename, and appends also ".csv". As I'm looking to create a file titled "DataLog20200318.csv" and of course based on a trigger develop a few different data log file with unique timestamps. My code works fine when providing
Code:
File dataFile = SD.open("datalog.csv", FILE_WRITE);
Code:
char fname[] = "DataLog";
char tmstmp[21];
sprintf(tmstmp,"%d%02d%02d%02\0",year(),month(),day());
char fext[] = ".csv";
strcat(filename, fname);
strcat(filename, tmstmp);
strcat(filename, fext);
I guess I don't understand the road block I'm running into... Am I overlooking some simply character array size or what is it?
Here is the demo code I tried from the Data Logging demo that works until I change from a static text to the filename variable:
Code:
#include <SD.h>
#include <SPI.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// Teensy audio board: pin 10
// Teensy 3.5 & 3.6 on-board: BUILTIN_SDCARD
// Wiz820+SD board: pin 4
// Teensy 2.0: pin 0
// Teensy++ 2.0: pin 20
const int chipSelect = BUILTIN_SDCARD;
char filename[26]= "";
void setup() // ************************* SETUP *************************
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Initializing SD card...");
//=================================================================================
// establish the file name with timestamp and extension
char fname[] = "DataLog";
char tmstmp[] = "20200318";
char fext[] = ".csv";
strcat(filename, fname);
strcat(filename, tmstmp);
strcat(filename, fext);
Serial.println(filename);
//=================================================================================
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open(filename, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
Thanks, your frustrated programmer