Simplest way to divide up a project / sketch?

Hi, relatively new to Arduino here and re-familiarizing myself with C++ after a long time.

I'm trying to figure out the simplest way to divide my project up into discrete parts, including use of classes and/or function pointers but with as little boilerplate as possible.

Given how the build process works, I think that might mean moving my loop() and setup() functions into a file late in the alphabet (e.g., z.ino). In that case, anything defined at file scope in another file will be available to it. If I then use the main sketch file as a place for globals and assume intermediate files won't have access to each other, should work?

Presumably the alternative (less hacky, but lots of extra code and files) is to just abandon .ino files and go with .h and .cpp and explicit #includes.

Any recommendations?

that is my recommendation.

a7

You can have one main .ino file and put setup() / loop() there. The rest of the modules would then be done with .h / .cpp files. I avoid the multiple .ino file hack. See my Reply #5 Here for some basic guidelines.

A different approach to easy BU, just one folder to ZIP

Duplicated libraries - Using Arduino / Programming Questions - Arduino Forum

also
Sketches and main() - Using Arduino / Programming Questions - Arduino Forum

also
Using Modern C++ Techniques With Arduino | Hackaday

also (NO SETUP?MAIN)

Check out this link:

Gammon Forum : Electronics : Microprocessors : How to avoid the quirks of the IDE sketch file pre-preprocessing

It explains the many options you have to completely avoid .ino files, if you want to.

Arduino programs are generally pretty small. I find it easy enough to put it all in a single file, especially when I'm starting a project. I might use .h and .cpp if I have written a substantial class, but that's a rare occurence.

I view it like using multiple Arduinos for a project: don't do it until you have to.

Yes. It's called "the soup" or just "soup", and I exploit/depend on my text editor's features to deal with it.

Even just using one cpp file… I have been burned by the ino preprocessor and I think it rare to get through much work without experiencing an oddness.

It has been improved, but that may have been a low bar.

a7

If you foresee yourself building anything particularly complex, I'd suggest avoiding the Arduino IDE altogether and installing Visual Studio Code and the Platformio plugin. It's a far better development environment.

Never had an issue with .h / .cpp modules. The automatic prototype generator doesn't mess with those. And even in the (single) .ino file I provide my own prototypes for all functions so auto generator doesn't do anything.

Yes, I was not clear. I meant using one cpp file for everything or nearly, instead of one ino for everything.

Because the ino, even just one, can give problems. First time I got srsly burned it turned on having added a comment to my code… which is the purest of irony as I don't use comments much at all.

As you say, using cpp and function prototypes steps aside of any IDE build process. And just leaves you in the normal world of pain which is programming.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.