Separating out code blocks from sketch

I am using Arduino 1.0 to code and upload the sketch.

I have a number of tabs, each containing different 'functions' ( correct term ? ), like void LogItC(int LineEnd){ etc

However, my main sketch has a lot of sections that I consider non-essential to the whole applications purpose ( like the section that reads the temperature sensors and updates the LCD display ) and also sections that I would like to get out the way to un-clutter my sketch.

Is it possible to separate these code blocks to a separate tab without having to include them in a function, and have some single code line to "include tab A code here" in the main sketch ?

My thinking to not have them in a 'function' is so that the separated code can still use all the variables from the main sketch.

Where would you put those #include statements ? I mean, in the middle of the current code (like "folding" a code block) or at the top of the sketch ?

PS: I think you'll have more chances to get help if you post your entire code...

I do that by finding logical places to split the sketch across tabs
first tab might just be comments on what the sketch does, and is the overall filename
next is a_presetup with the #include statements and variable definitions and pin assignments
then b_setup
then c_void_loop
then d_void_loop_2
then e_void_loop_3
etc.
Final file will have the last } that closes out voild loop() {

Not the prettiest, but works for me & I can find stuff.

@CrossRoads

you mean your loop() {} is split across different files ?!? Aaargh :grin: :grin:

1 Like

@CrossRoads

Are you saying that when compiling for upload, it will join all tabs in alpha numeric name order ?

If so, am I correct to assume that variables would be kept alive between tabs, except for those in separate coded functions ?

DaveO:
Is it possible to separate these code blocks to a separate tab without having to include them in a function, and have some single code line to "include tab A code here" in the main sketch ?

Yes, but don't go there. It will be a complete mess.

Any design that has a single function so big it has to be split over multiple files needs major rework.

Think "black boxes". Make modular functions that achieve one thing, and where possible get passed down one or more arguments and return a result.

You can share global variables over multiple files, if necessary. Declare the variables in one place (eg, the main sketch) and then put then "extern" into a .h file.

eg.

Main sketch:

int a, b, c, d;
long e, f, g;

.h file:

extern int a, b, c, d;
extern long e, f, g;

Now any file that includes the .h file can refer to those variables, and the linker will redirect to the "real" ones.

Really, as a rule of thumb, if a function doesn't fit on "a page" (and I know that is loosely defined) it is too long.

The tabs are all recognized as one file.
I only declare variables globally, so they all end up in the pre_setup tab.
The tabs all seem to compile in alphabetical order.
To be honest, I have not written a function yet (since Aug 2010), I generally have my code set a flag, for a section farther on, if the section farther on sees the flag it does its thing and clears the flag.
Sort of like a function I guess.

Helps me to cut down on scrolling up & down pages.