When in the Arduino IDE I see that there are multiple tabs at the top for different sketches. It seems to me that when I hit upload it should only upload the current tab I’m working in but some of the error messages I’ve gotten say things about not being able to find or upload code from a different tab so I guess my question is ‘does IDE try to upload all the sketch tabs and I need to open a new instance/window every time I want to work with a single sketch?’
Please tell me if I’m not making any sense. I’m brand new to this and don’t quite understand a lot of what I’m doing.
I think the more accurate way to put this is that there are multiple tabs for different source code files that constitute components of a single sketch.
One sketch folder can contain several files. The most common arrangement is a single ino, a single .h and multiple cpp. It can also be multiple ino but that comes with special rules and offers no advantage that I know of.
These arrangements are for normal user sketches NOT library development.
Doesn't require an understanding of the concept of "translation units"
I accept that some advanced users will not consider either of those to be advantages. However, for users at an intermediate experience level wanting to split up a sketch project into multiple files without having to first learn additional fairly complex programming concepts, the multiple .ino files approach may be a good option.
I’m not even intermediate yet, I just started this week and overall I’m having a blast and enjoying learning all this stuff. a few things are frustrating and confusing but I’ll keep trying and asking for help so I can get better.
All .ino files are concatenated into a single file. The generated function prototypes are added at the top of that file.
So you can call any function from any of the .ino files at any point in the sketch, without needed to manually add a function prototype/forward declaration before that point, exactly the same as how it works if all the code is in a single .ino file.
As for manually added declarations, you can reference the declared object at any point in the sketch after the declaration, just the same as how it works if all the code is in a single .ino file.
I had the same question about opening a new sketch - why is a new instance window of the IDE opened instead of adding a new tab to the current instance but I guess opening a new instance of the IDE is necessary or expected behavior.
Regarding the tabs in any given instance, I have ESP32_SPI_9341.h open in the second tab when I open some sketches - I don’t know why that .h file is occupying the second tab, does it have to be? How do you close a tab? I only see “delete”. Not sure what exactly that does. Do I have to open any of the .h files that are #included in the sketch (do they have to be present in other tabs) ?
A second copy of the IDE opens if you open a second sketch (folder).
If you have an .ino and a .h file in the sketch folder, they will get opened in separate tabs. There is no need to close the second tab; it is there as a convenience in case you need to edit it.
Most #included .h files are NOT in your sketch folder, so do not create a tab. Those are part of the library system.
I did a test to check, and YES if you delete a tab you delete the underlying file, so be very careful with that.
Another related thing you may find handy is after and only after a successful compile you can then highlight/click an include and the file will be opened READ ONLY (the LOCK icon) in a new tab. That tab will have a close X at the right edge. Useful for seeing what member functions are available for a class, but there is another method too (clue start typing)
The reason is that a sketch may contain multiple files. The code from all the files of the sketch is compiled, and that compiled program uploaded to the Arduino board. If multiple sketches were opened in a single Arduino IDE window, it would become difficult to understand exactly what code is being compiled. This would result in two types of confusion:
Users who think the IDE will magically merge the multiple sketches together and upload the amalgam.
Users who think the IDE is only compiling and uploading the code from the specific file they have selected, rather than all files of that sketch.
The benefit of Arduino IDE's window-per-sketch design might not be obvious if you have only worked with sketches that consist of a single file, but the Arduino IDE design must accommodate multi-file sketches.
It is certainly technically possible to design an Arduino IDE application so that it supports having multiple sketches open in the same window. So "necessary" is not the right way to describe it, but "expected" is. This is the intentional design of Arduino IDE, with the goal of making sketch development intuitive.
Arduino IDE creates a tab for each of the code files in the root of the sketch folder.
For .h files in the root of the sketch folder, yes.
If your sketch has supplemental files that you don't wish to have open as tabs by default, you can move them under a subfolder of the sketch named src.
You can organize the files in any arbitrary subfolder structure you like under that src subfolder. Arduino IDE compiles the contents of the src subfolder of the sketch recursively.
Arduino IDE automatically opens all the sketch code files present the root of the sketch when you open the sketch. It does not do this for the files in the subfolders. You can open an close those files on demand. I'll provide instructions you can follow to open a sketch code file from a subfolder of the sketch:
Select File > Preferences... (or Arduino IDE > Settings... for macOS users) from the Arduino IDE menus.
The "Preferences" dialog will open.
Check the box next to "Show files inside Sketches" in the "Preferences" dialog.
Click the "OK" button.
The "Preferences" dialog will close.
Click the folder icon in the activity bar on the left side of the IDE window to open the "SKETCHBOOK" view.
You will see a listing of the contents of your sketchbook folder. Click the ❯ icons to the left of the relevant entries to navigate to a code file from the current sketch that you want to open.
Click on the file in the "SKETCHBOOK" view.
The file will open in a new editor tab. You can browse and edit the contents as needed. When you no longer need the tab, click the X icon on the tab to close it.
It is not possible to close the tabs for the files in the root of the sketch folder that open by default in the Arduino IDE editor. The reason for this design is that new users could get confused by the fact that Arduino IDE is compiling and uploading code from files they don't even see in Arduino IDE. By showing tabs for the primary sketch code files, and only those files, Arduino IDE intuitively communicates which code will be compiled and uploaded to the user.
The tabs on the top are not multiple sketches. They are external files for the current project. A single arduino ide instance can only have one sketch.
These external files are only the ones that are referenced in your program but not the ones in a library.