Ok for anyone looking at this thread here’s the solution I settled on. I added this code to the beginning of my void loop() program and this allowed me to type in a file name to start the data gathering and use that file name for the file on the SD card. Works! What I did was to convert my string to a char array and add a null terminator to make it like a char* declaration. First the added code then the whole program:
str_len = inputString.length();
inputString[str_len] = ‘\0’;
char char_array[str_len];
inputString.toCharArray(char_array, str_len);
I hope this helps someone…
/*
- SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK(SCK) - pin 13
** CS - pin 10
*/
#include <SD.h>
#include <SPI.h>
String inputString = “”; // a string to hold incoming data
char* temp=“File1.txt”;
boolean stringComplete = false; // whether the string is complete
const int chipSelect = 10;
const int sensorPin = 0;
int lightLevel, high = 0, low = 1023;
float ti = 0.0;
int str_len =0;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print(“Initializing SD card…”);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
return;
}
Serial.println(“card initialized.”);
}
void loop()
{
str_len = inputString.length();
inputString[str_len] = ‘\0’;
char char_array[str_len];
inputString.toCharArray(char_array, str_len);
// temp=char_array;
// print the string when a newline arrives:
if (stringComplete) {
// here we go
for (int i = 0; i < 51; i++){
File dataFile = SD.open(char_array, FILE_WRITE);
lightLevel = analogRead(sensorPin);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(ti);
dataFile.print(",");
dataFile.println(lightLevel);
dataFile.close();
ti=ti+.05;
}
// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening datalog.txt”);
}
delay(50);
}
//done
Serial.println(temp);
Serial.println(char_array);
// clear the string:
inputString = “”;
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == ‘\n’) {
stringComplete = true;
}
}
}