Let's Make Robots!

Organising your code in the Arduino IDE

Similar to collapsing code
Mr_Tidy_MkII_Sample_Code.zip19.9 KB

I have been revamping the code for Mr. Tidy and it has grown large enough (23Kb when compiled) to be a real headache to scroll through.

I know that some IDE's solve this problem by collapsing code but the Arduino IDE doesn't have this feature. The Arduino IDE has "Tabs" instead which I have not seen people use very much. In the past I have used these to seperate constants and IO pin definitions but you can also break your code into logical sections and store those sections in the tabs making it easy to jump from one section to another.

In my new code for Mr. Tidy many of the larger functions now have their own tab. I have grouped other smaller functions together into a logical group such as functions related to the gripper. See the attached code. I was a little annoyed that if you have a lot of tabs the IDE doesn't create multiple rows, instead they just disappear of to the side. Fortunately they are still fairly easy to access from the menu or using hot keys.

Note that my code segment tab names do not have an extension and do not need to be included at the beginning of the program.

Organising your code in this way makes it much easier to edit large programs. When you compile your code the IDE just treats all the seperate segments as one.

Good luck and enjoy!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I just tried this, workjs well, but make sure to keep all you variable definitions in the main tab.

Yes Global variables need to be before the Setup() function. Local variables are not affected unless you split the function they are in which would not be sensible at any time.

I use classes and objects so I use a lot of tabs (two tabs for each class - one for the header file and one for the code file, .h and .cpp) one for the .pde file and one tab (.h) for general definitions of the project. I also use tabs to group some generic function not related to classes but the main program or for tests.

As an example I have 14 tabs for cr4's motor controller's 5 classes.

It may not worth for small projects but I am used to work this way since I've done a lot of C++ programming.

Hey, this is really great tip and feature of that IDE.   I've never used it.    It almost sounds like libraries but instead they are all subroutines within the same file.    Definitely large programs need something like this.

When it compiles the program, how does it order the routines, or is that problem a non-issue because each tab MUST be a standalone routine?    with 1 tab as the Main section, etc.

The order does not matter too much although I would definitely only use tabs for whole routines or groups of routines.


function calls, procedures or subroutines have a specific box that is then broken out into its own flowchart. So, if one were to flowchart their programs for the arduino and then use the flowchart to write the program, the tabs might actually make some sense.

You are missing the point. Flow charts are completely useless for large programs unless you are going to draw on the walls.

When you're program gets so big that you need to scroll through hundreds of lines of code to find one line then you need a way to quickly jump to the relevant piece of code.

Collapsable code does this by allowing you to collapse an entire function into one line and only have the sections you are working on expanded.

The Tabs work in a similar way by allowing you to put one or more functions into a logical block and jump between the relevant functions.

For example, if I am modifying the Collision Avoidance function and need to check if it will affect the ObjectDetectAction function then I can just click on the relevant tab, straight to the relevant code. When I jump back to the tab I was working in then the cursor is right where I left it on the line I was working on.

This is much better than spending a minute or two scrolling through a huge pile of code looking for the section you need to check and then spend another minute or so scrolling through the code to find where you were.

I only meant that individual tabs for each procedure/function does make sense and therefore it kind of makes sense that the IDE breaks things out that way.

PS- WAY off topic

Re: flowcharting or other forms of psuedocoding

While I have no direct knowledge of large programs or working on software in that scene, my education has said that there will be some form of psuedocoding done, no matter the size of the project.

Don't do this but... I have been a programmer for over 25 years.  I've written several large systems from the ground up.  I never flowchart or psuedocode.  Well I have psuedocoded a couple times when things got really tricky.  But, for the most part, I do it all in my head.  I just don't see much difference between psuedocode and just code.  C++/Java.

Ok now I understand.

It is not always efficient to have a tab for each function. For example I had four small interrupt handling routines that would not be practical to put in seperate tabs. I grouped them together.

The callibration routines do not run under normal opperation so it was more logical to group them into a seperate tab.

I was thinking more along the lines of the picaxe flowchart programming system. While a good idea for children it is not practical for any reasonable sized program. I tried putting the caterpillar code into the flowchart format and I was scrolling and zooming in and out like crazy just to copy the code.

True you can psuedocode for any language but in that case it is more a generalised map of the programs over all function. Using that way of thinking then you could use one tab for each large block of logic with each tab holding the functions related to that logic block.