I have read that the Setup section is nothing special, it's just another function that happens to be required by the IDE.
So I'm wondering if many of the startup commands required by some libraries can be executed elsewhere as long as it's only done once.
Here's why: I have a bunch of common functions that I usually keep in a special tab that gets "included" into the main sketch. Everytime I put that special tab into a new sketch I have to remember to insert the associated startup commands into the Setup section. I would rather have each function in that tab execute their Setup commands one-time automatically when they are first called.
Or, alternatively, maybe put all those startup commands into one setup_all() function in that special tab where all the setup commands would be executed. That way I only need to put ome setup_all() command into every sketch that use that special tab.
Even the often used Serial.begin() might be included into the setup_all() function.
I recognize that there may be reasons to keep some startup commands out of the setup_all() function, but as a general concept is the above viable?
I can think of some ways to hack this, but I don't think there is ultimately a good solution to this. You would need to code every one of your common functions to check if they need to be initialized every time they are ran, and that will have a small performance penalty.
Setup is really the best place to put that stuff. Trying to clever your way out of this will probably only confuse you even more.
Maybe modifying the default BareMinimum sketch will work for you; it's located (on a windows system) in C:\Program Files (x86)\Arduino\examples\01.Basics
Modify it to include your call to setup_all() (or whatever) in setup(). If your special tab is not part of your sketch, the compiler will bark at you because it can't find te function.
Thanks, everyone for the thoughts, comments and suggestions. I sounds like both of my two techniques are basically viable.
I'll probably end up using both techniques, but I favor using the technique where the function in the "special" tab initializes itself one-time, the first time it is invoked. I don't like the idea of initializing all the "special" functions unless I'm actually using them. Therefore I need an efficient way to determine the first time a "special" function is invoked, since that determination will be made every time that function is invoked.
So, does anyone have any code suggestions that would be efficient? I guess minimizing cpu cycles would be most important to me. I'm guessing a simple "if, else" statement using a single char variable to remember the state is good enough, but I'm sure the people here have better ways.
Again, thanks to all for their contributions.
Frank
When i say "invoked" I do mean from Setup or Loop or possibly from another function as you suggest.
The only savings I am getting is my sanity. Basically, it's making it easy for me to plop-in a bunch of frequently used custom and common functions without having to explicitly code the initialization of them.
This may seem like a trivial savings to the savvy users here, but for me somehow I think it's significant.
Really the only way to do this is to have a boolean variable in your library that tracks if the initialization has been done. At the beginning of every function in your library, you check that value and if it's false you run the initalization function, which will set that variable to true for all subsequent function calls.
static bool isThisThingOn = false;
void Initialize()
{
// do whatever here to set up all your stuff.
isThisThingOn = true;
}
void DoTheThing()
{
if( ! isThisThingOn ) Initialize();
// do your fancy cool stuff here.
}