Basic problem, but Im not enough of a brain to figure it out.
I want to create a file on SD card on boot. To preserve older files from being overwritten, I want to enumerate them and use only the highest enumerated file for reading data from later on for display on an OLED.
The createNewFile function already returns the name of the new file - cool - but if I want to call that function it also has to be within the void setup function.
So I could move the createNewFile function up above void setup, but that places it above the code that initialises the SD card itself. The createNewFile can't access the card before it's been initialised.
So, very simply
#include blah blah
void setup ()
{
//Initialise SD Card here
}
//Declare createNewFile here
// return fileName
}
//can't print filename to serial here because it's not within the braces
void loop ()
{
//Print fileName to serial loops forever instead of once if placed here.
}
So simply, in fact, that it no longer makes sense, and doesn't demonstrate your problem.
Peering into my crystal ball, I perceive that you are trying to declare a string variable to hold a file name and give it an initial value which is an expression which includes some value returned by a function called in setup(). You can't do that. Initial values can only be based on values known at compile time (before the code is uploaded to the Arduino).
Instead, you need to declare the string as an empty, global variable, before it is referenced in setup(). In setup(), you can then dynamically build the file name before using it to open a new file. The sprintf() function could be useful for that.
C++ does not work reading and interpreting the code file sequentially. even if your function is declared/defined before setup() (and there are good reasons it should), it doesn't mean the code inside the function will get executed before setup()...
whilst f1() depends on Serial to be initialized if you want to see something on the Serial monitor, it's not called before setup().
setup will be called by the Arduino environment (main() function calls it), which will call f3(), which will call f2(), which will call f1(), which will print Hello.
(the compiler might optimize things out but conceptually that's what's happening).
RTC is one solution ($1.38USD), NTP is another.
You seem to be oblivious to the fact that every time that sketch starts, it starts over at 0. Time based is the way to go.
It doesn't mean that the code overwrites old files. The code test the existence of the file with given number and increment the counter until the free file number will found.
I agree with @kolaha that using a timestamps is not need here by task assignment
So OP can get 65536 files (some with a dash because the int will rolllover) and after that it will be stuck forever in the while loop on a 8 bit arduino but no file will be overwritten. That’s roughly 180 years worth of file creation if you create one per day…
A DS3231 keeps the year on 2 digits so will have troubles around 2100 and need extra coding to deliver the same 180 years…
(Assuming the need is still there (and OP too) in 100+ years… I won’t …)
Truth to be told, the file name might create issues if you need an 8.3 format. In that case something like 20240813.log would work better as the size won’t change for some millenials!