Using tabs in Arduino IDE

If you plan to write a moderate to long program in Arduino IDE, you should learn to use tabs to keep your codes organized. Let’s talk about this issue in some details.

First, you should have a plan, what kind of problem you are trying to solve with your program, how you will solve it. But most of the time we only had a rough idea what we are going to do and as we are doing it our programs grow bigger and messier. OK, if you follow my suggestions on how to use tabs (i.e. separate your one giant file into several smaller files), you will survive:

You should try to cut your program into smaller pieces, so that each piece represents a specific set of functions. Say you have a sonic ranger in your setup. Then you will likely have sonic ranger reading functions, displaying distance from reading, displaying speed from distance readings. These functions are better off separated from those that read and display value from a temperature sensor.

You can keep all these pieces in your main program file but soon your main program becomes too long and you find yourself scrolling the side bar up and down, looking for one bit of program to modify.

Here is how to separate your program into several pieces, each having its own file name.

  1. Press this button to drop down a menu, then select new tab.
  2. On the bottom of the arduino IDE window, type a name descriptive enough, like measure for all your sensor measurement functions.
  3. Cut your sensor measurement functions from your main program, and paste them in the newly created file, which is now a tab inside arduino IDE.
  4. To make matter even more clear, add several lines of comment describing what these functions do. This also helps you organize your functions.

Here is an explanation of what you have just done: if you write codes in separate .pde files, you need not worry anything regarding whether one function in one file knows the other functions defined in other files as all .pde files are concatenated into one file before arduino IDE compiles everything so it the same as writing all those lines of code in one file, just more organized.

Essentially, you should only keep all variable definitions, setup() and loop() in your main .pde file, and disperse all your functions into their separate .pde files, like this one in picture.

Starting here, you will have to seriously programmed for quite some time to hit another road block to need my following help:

Say you have succeeded in creating many hundred lines of code. You have also come to understand the importance of using #define and PROGMEM. The head of your main .pde becomes annoyingly long like this but only more lines:

#ifdef stand-alone
// Stand-alone photogate setting
#define button1_pin 11 //enter
#define button2_pin 13 //down pause
#define button3_pin 12 //up unpause
#define button4_pin 10 //Escape
#define gate0_pin 15
#define gate1_pin 14
#define gate2_pin 17
#define gate3_pin 16
#endif

#ifdef shield
// Photogate shield setting
#define button1_pin 12
#define button2_pin 11
#define button3_pin 10
#define gate0_pin 3
#define gate1_pin 2
#endif

#define pulley_spokes 10 //16 spokes are evenly laid on the pulley.
#define storage_size 100
#define rotation_update_time 1000000
#define two_pi 6.2831853
//#define vol_a_val (10.0000/1024.0000) // This maps -5V to +5V into 0-5V with a bipolar to unipolar converter.
//#define vol_b_val (-5.0000)
#define vol_a_val (5.0000/1024.0000) // This is the default mapping for internal AD converter.
#define vol_b_val (0.0000)

Now you are contemplating on adding just another tab, called includes.pde

In that tab you type : #define haha 7

In your main program you have int data=haha;

You compile, and an error pops up:

What the ?$%^& You defined haha but the IDE fails to recognize it? You quit coding and go on and forget arduino all together. Maybe you should read on for the source of the error instead:

First do int data=7; so you can complete the compile. Then take a look at the temporary file arduino IDE generated under:

C:\Users\owner\AppData\Local\Temp\

Under a folder called build1555865782840320506.tmp or like, you can find all the files generated by arduino IDE while compiling.

You can find more about these files on this blog post: http://smileymicros.com/blog/2011/02/10/where-is-the-arduino-hex-file/

Open that .cpp file that is named the same name as your main .pde file and look in the very end you will find the answer:

#include “WProgram.h”
void setup();
void loop();
int clock = 6; // Define encoder pin A
int data = 7; // Define encoder pin B
int count = 0; // pre-init the count to zero
int c = LOW; // pre-init the state of pin A low
int cLast = LOW; // and make its last val the same – ie no change
int d = LOW; // and make the data val low as well

void setup() {
pinMode (clock,INPUT); // setup the pins as inputs
pinMode (data,INPUT);
Serial.begin (9600); // and give some serial debugging
}

void loop() {
c = digitalRead(clock); // read pin A as clock
d = digitalRead(data); // read pin B as data

if (c != cLast) { // clock pin has changed value… now we can do stuff
d = c^d; // work out direction using an XOR
if ( d ) {
count–; // non-zero is Anti-clockwise
}else{
count++; // zero is therefore anti-clockwise
}
Serial.print (“Jog:: count:”);
Serial.println(count);
cLast = c; // store current clock state for next pass
}
}

#define haha 7

Aha! The content of your includes.pde is concatenated to the END of your main .pde file! Have you heard of “define before use”?

This is what arduino IDE does, it concatenates all .pde files, starting with the main .pde then in alphabetic order. So if you define something in a .pde file or tab, then the definition appears after the main .pde and the defined entity CANNOT be used in main .pde or any other .pde file that is alphabetically ahead of the file that has the definition. DARN IT!

How do you fix that?! Maybe return all the #define lines to the main .pde and curse the arduino IDE and get over with it?! You should simply choose to read on.

To really think out of box, we need to separate those pesky #define lines from our beautiful main .pde, and we need those #define lines to be compiled AHEAD of the main .pde! That is right, AHEAD of the main .pde. You are thinking in the correct direction. To redirect the order in which the compiler compiles, you can use a HEADER file! Have you not programmed in the old-fashioned C? Always have “#include <stdio.h>”? Don’t worry, we’ll get there.

Now let’s change that includes.pde into includes.h

Then in the beginning of your main program, add the following magical line: #include “includes.h”

Then modify your code so again you have int data=haha;

Compile, succeed!

Here is the temporary output file:

#include “includes.h”
#include “WProgram.h”
void setup();
void loop();
int clock = 6; // Define encoder pin A
int data = haha; // Define encoder pin B
int count = 0; // pre-init the count to zero
int c = LOW; // pre-init the state of pin A low
int cLast = LOW; // and make its last val the same – ie no change
int d = LOW; // and make the data val low as well

void setup() {
pinMode (clock,INPUT); // setup the pins as inputs
pinMode (data,INPUT);
Serial.begin (9600); // and give some serial debugging
}

void loop() {
c = digitalRead(clock); // read pin A as clock
d = digitalRead(data); // read pin B as data

if (c != cLast) { // clock pin has changed value… now we can do stuff
d = c^d; // work out direction using an XOR
if ( d ) {
count–; // non-zero is Anti-clockwise
}else{
count++; // zero is therefore anti-clockwise
}
Serial.print (“Jog:: count:”);
Serial.println(count);
cLast = c; // store current clock state for next pass
}
}

Notice the “#define haha 7” line is gone from the end of the temp file. But a line #include “includes.h” appears in the beginning of the file. The function of this include line is to insert the content of the included file i.e. “includes.h” right where the #include line appears in the file. This way, anything you write in the includes.h file appears magically before the main content of your main program, when it compiles.

We call #include a pre-compiler command or directive. It tells the compiler where to locate a file and insert its content at the spots of this very #include command. So if you have

Dear Sir and Madam,

#include <declaration of independence>

Thank you for your attention!

then the entire content of the declaration of independence appears in your speech, whether it’s relevant or not.

Now that you’ve spent so much time reading this post and learned a trick or two, you may want to read on to learn all the tricks.

With #include, we can put all the #define lines in our “includes.h” files, then add #include “includes.h” in the beginning of the main program, tada!

Maybe now you get a bit over confident and start to move variable definitions into includes.h

Sorry to tell you, that won’t work.

This post is already too long and we are wandering off topic. So to continue on the solution to your problem, please read on how I use different files to organize my project. See you on another post!

22 Responses to Using tabs in Arduino IDE

  1. forex says:

    thank you for this tutorial
    good job

  2. Pingback: Organize your code – big ASCII art font « Liudr's Blog

  3. hugo says:

    Thanks for that.

    I still have a long way to go but this is really helpful.

    Cheers

  4. Righty says:

    If the arduino IDE compiles in alphabetical order, can you name your includes “aaa_includes” and have it compiled first?

    Your example seems more correct, just wondering if my way would work.

    • liudr says:

      Yes, Righty. If you name with aaa then that file gets cancatinated right after the main pde file and compilition order can be manimulated that way. In fact before I went open source software with those libraries, I used that name trick to protect my code from unauthorized copy and cut 😉

  5. TCWORLD says:

    Thanks, this was very helpful! I only wish the people who make Arduino IDE would make it so that sections of code can be expanded and hidden like in notepad++, that would save the hassle of all these tabs.

  6. Owe says:

    How do i REMOVE a tab????

  7. Toby says:

    Perfect. Just what I needed. Thank you very much.

  8. Jacob Carstens says:

    How do you navigate between tabs in a big project. E.g. Marlin firmware https://github.com/ErikZalm/Marlin.
    I can not use left/right (My OS own them for rot. screen) and if I click it with the mouse nothing happens. And if I get a vertical file-list I can’t to the last file, it is not shown.

  9. Jacob Carstens says:

    Ups <!– –> disapeared

  10. Jacob Carstens says:

    control+alt+right and control+alt+left has been taken out of 070713 at 03:12 pm.

  11. petosoft says:

    Reblogged this on petosoft.

  12. wanek t says:

    very good explanation. great article!
    thank you!

  13. Question:

    How to move around the “hidden” on the right when too many ??

    Thanks,
    Rafa.

    • liudr says:

      Good question! You must be developing a large project. To move one tab to the right, use ctrl + tab. To move one tab to the left, use ctrl + shift + tab. It’s like inside a web browser.

  14. Pingback: manière correcte d'inclure les fichiers .cpp et .h dans une esquisse Arduino

  15. Pingback: modo corretto di includere file .cpp e .h in uno schizzo Arduino C++

  16. Tim Robinson says:

    Wow! Thank you so much for your time and help. This post has been most useful, as I am working on a real spaghetti bowl piece of code. Now I have a neat, organized project. Thanks for all the time you invested, trying to help others

Leave a Reply

Discover more from LiuDr Electronic Solutions LLC Official Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading