Long sketch code question

Status
Not open for further replies.

bossredman

Well-known member
My sketch is now getting quite long & to be honest - I'm getting tired of scrolling up & down.

I heard about TABS in teh Arduino & moved a lot of my functions to their own TAB.
This appears to have worked fine (fingers crossed).

But I wanted to move all my variable declarations to a seperate TAB too - but reading up on this suggested it couldnt be done without a lot of work (which was way beyond my understanding) - if at all.

But I tried this:

Moved the void setup() code to a TAB called A_Setup
Moved the void loop() code to a TAB called B_Loop
left all the declarations on the original "sketch" named tab.

This has compiled OK & "appears" to be working OK.

Does this sounds acceptable or am I taking a big risk & may run into some unexpected beahviour.

Thanks
 
you can move functions to other tabs, functions are accessible from any tab, in any order
however, defining global variables, they have to be higher than the functions using them.
ex.
if a function on tab2 and a function on tab 7 are using a global var, the variable has to be declared before the tab2 function, so tab2 and tab7 have access
if you put the variable on the 3rd tab, function 7 will work, but you will get a compile error, because it's not declared for a function in tab2 (since you declared it on tab 3.
declaring it in tab1, of course, tab2 and tab7 are after it, so thats why it'll work.

if you need a persistant variable for a function, you can use static inside the function or global just above the function to keep things neat, knowing that no other code behind it will ever use it.

obviously, const char*'s (you set them for a reason) and includes, those obviously go straight at the top of tab1
 
OK thanks.

Seems like after you save, close & re-open a sketch, the IDE re-orders tabs after they've been named into alphabetical order (with the exception that the tab with sketch name is always first).

So when you refer to tab numbers above is that teh order AFTER a close & re-open or as they are created.?
 
Why doesn't arduino ide allow you to expand/collapse blocks of code.
I attempted to organise by separating logically into different tabs but failed for the above reasons.
 
OK thanks.Seems like after you save, close & re-open a sketch, the IDE re-orders tabs after they've been named into alphabetical order (with the exception that the tab with sketch name is always first).
That is also the order the contents of the tabs are placed in when they are combined into one big honking .cpp file for compilation. Keep that in mind when considering the scope of global variables.
 
So yes my understanding is that you have to think about tab names to keep the order right come compile time. And yes being able to fold up blocks is one of many features missing from the IDE, both for simplicity for first timers and to reduce cost on a free project. If you have a favorite IDE there are a range of methods to have Arduino compile from Notepad++, Eclipse, MS visual studio and others if you have one that fits your mental model.
 
Status
Not open for further replies.
Back
Top