Naming a text file with date and time

I have a program that receives data as it is posted in the serial port of an Arduino program, and as the data is received, it saves to a text file. This part works fine.

I want to create a textfile with the date and time at the start of the program, and save all subsequent data to that same file for the duration of that run, however, it creates a new text file every minute. I have tried every combination that I could think of to fix this, but for some reason, the saveStrings() function will not accept the name of a string variable as an argument.

Here is my code below that works, but creates a new .txt file every minute

//(dataOutput is the name of array to which each new line of data is appended)
void setup(){
    output = createWriter(month()+"."+day()+"-"+hour()+"h"+minute()+".txt");
}
void saveData(){
    saveStrings(month()+"."+day()+"-"+hour()+"h"+minute()+".txt", dataOutput);
}

This is how I want it to work

void setup(){
    String title = month()+"."+day()+"-"+hour()+"h"+minute()+".txt"
    output = createWriter(title);
}
void saveData(){
    saveStrings(title, dataOutput);
}

I have tried a multitude of things in the saveStrings(String, String[]) function with the first argument being: output, dataOutput, PrintWriter, PrintWriter(title), createWriter(title), etc.

Does anyone know of a solution? I have only been using Processing for a week, so I feel like I am floundering around in the dark.

Thanks in advance!

Answers

  • Answer ✓

    What error are you seeing?

    The code you posted won't compile. You declare the title variable in your setup() function, which means it will not be visible outside of that function. However, you then try to use the title variable in your saveData() function.

    You also don't have a declaration for output or for dataOutput.

    Fix your code so that all of your variables are in scope when you need them to be.

  • edited July 2015

    You did it! Thank you! I changed the 'title' variable to a global variable, and it works perfectly now.

    I didn't put the whole code up because it communicated with the Arduino's serial monitor, and I didn't want to complicate things.

    But, thank you again. You gave me the information I needed!

Sign In or Register to comment.