Hi @flynace.
Just in case you aren't already aware, I'll describe the way to add a "tab" to your sketch:
- Click the ●●● icon (or if you are using Arduino IDE 1.x, the ▼ icon) at the right side of the tab bar in Arduino IDE.
A menu will open.
- Select "New Tab" from the menu.
The "Name for new file" dialog will open.
- Type the name you want to use for the tab.
- Click the "OK" button.
The dialog will close and you will see that a new tab has been added to the sketch.
It is important to understand the way the code in tabs is handled:
https://arduino.github.io/arduino-cli/latest/sketch-build-process/#pre-processing
The Arduino development software performs a few transformations to your sketch before passing it to the compiler (e.g., avr-gcc):
- All .ino and .pde files in the sketch folder (shown in the Arduino IDE as tabs with no extension) are concatenated together, starting with the file that matches the folder name followed by the others in alphabetical order.
The order of concatenation can be important. For example, let's say you created this sketch:
SuperCounter/
├── Loop.ino
├── Setup.ino
├── SuperCounter.ino
└── Variables.ino
SuperCounter.ino
/*
This is my amazing counting sketch.
It is organized into tabs. The code is in the other tabs.
*/
Loop.ino
void loop() {
foo++;
}
Setup.ino
void setup() {}
Variables.ino
int foo;
The sketch will fail to compile:
Loop.ino:2:3: error: 'foo' was not declared in this scope
foo++;
^~~
The reason is the order of concatenation. The code is concatenated into a single file before compilation, in this order:
─ SuperCounter.ino
─ Loop.ino
─ Setup.ino
─ Variables.ino
So the compiler sees this code:
/*
This is my amazing counting sketch.
It is organized into tabs. The code is in the other tabs.
*/
void loop() {
foo++;
}
void setup() {}
int foo;
The foo
variable is referenced at line 7:
foo++;
but it is not declared until line 10:
int foo;
So the naming of the additional tabs is important. A technique you can use to control the order of concatenation, while still having freedom to give your tabs descriptive names, is to add a numeric prefix to each of the additional tab names:
SuperCounter/
├── 10_Variables.ino
├── 20_Setup.ino
├── 30_Loop.ino
└── SuperCounter.ino
I chose to increment the prefix by 10 for each tab so that I have room to insert additional tabs later without having to change the names of any tabs that should go after the new tab.
Now the concatenated code will look like this:
/*
This is my amazing counting sketch.
It is organized into tabs. The code is in the other tabs.
*/
int foo;
void setup() {}
void loop() {
foo++;
}
and it will compile.
Arduino IDE displays the tabs in their order of concatenation.
It is not required. Arduino IDE will automatically give the added file the .ino
file extension if you don't specify a file extension in the "Name for new file" dialog. You only need to specify the file extension if you want the added file to have an extension other than .ino
.
Arduino IDE 2.x does have a feature like this. However, this is just something that comes built-in from the framework on which the IDE is built, not something the Arduino IDE developers implemented, and unfortunately it is buggy. But if you want to give it a try, I'll provide instructions:
- Press the Ctrl+Shift+P keyboard shortcut (Command+Shift+P for macOS users) to open the "Command Palette".
A menu will appear on the editor toolbar:
- Select the "View: Toggle Outline" command from the menu.
ⓘ You can scroll down through the list of commands to find it, or type the command name in the field.
An "OUTLINE" view will now open in the left side panel of the Arduino IDE window.