Where to find Information on how to organize larger Arduino projects?

Hello!
I am new to the world of Arduino. And I like it. I have some AVR assembly done more then 10 years ago.

But I like Arduino, because I must not do all the work for all the common things like LCD and so on.
I had running displays and stepper motors within minutes.
My first LC-Display driver in assembler (10 years ago) took me several days until it worked.

Now I can concentrate on my programms and not all of the details, that i can do but are boring to do.

Now here comes my question:
Is there somewhere a good source of how to organize larger Arduiono projects?
I found a lot of tutorials for small programms but nothing about this problems.
I know I could make libraries. But thiss is pure C++, and I would like to stay in the world of normal sketches.
How can i organize things like: declaring hardware pins, initialize display, get all the hardware in a controlled start-state. And so on.
If I divide my programm in several files. In which order are the compiled. What is with inkluding libraries that I need in different files?

Thanks in advance for any help
Jan

If I divide my programm in several files. In which order are the compiled.

It doesn't matter. All the individual files are compiled, producing object files. Then, the linker is called to link them together. It's a multipass linker, so the order of the files presented to it is irrelevant.

What is with inkluding libraries that I need in different files?

I'm not sure that I understand the question. It's a useful technique is all I can say.

Start with a blinking led, and try other examples. You will find the answers along the way.

Arduino use C++, but also the AVR C-code can be mixed with it.

You can create new C++ files (use just a name) and also header files (name them as *.h) to your project.
Use the drop-down on the right of the tab bar in the Arduino IDE.
You can even copy *.c and other *.h files in the folder of your project.

To use a library, the library must be present, and the include file must be included.

You don't have to include your own include files.
But sometimes you do. If the compiler uses a wrong order, you can also include your own include file in the first sketch.

You can break your code up into several .h files and #include them from the main sketch, simple but
helps keep different bits of code separate, and allows simple cut/paste reuse in other sketches.

But often you want to properly package some stuff (a driver for a particular chip), and the library
is the natural way to do this - in particular you don't need to copy it to use in another sketch, and
it is the sensible way to share code with others to use/improve/extend.

Thanks for the answers.
But that was not really my question. I try again to make it clear.

I know how to blink an led.
I know how to use (and write my own) library.
What I talk about is a really large project (A controler for a special CNC-machine) with:

  • more then 10 steppers and servos.
  • other subsystems with serial connenction
  • a touchscreen with UI (menues and so on)
  • Lot of switches and sensors
  • On my Mega 2560 all IO-Pins are in use
  • ..........

So we talk about more than a few tousend lines of code.
At startup everithing has to be initialised in the rigth order and the correct timing.

In other languages I would put things in classes to get it done.

How do you organize such a project with Arduino.

Thanks again
Jan

In other languages I would put things in classes to get it done.

How do you organize such a project with Arduino.

Why are you assuming that you would do something different because it's an Arduino? You wouldn't.

I have found what I searched for:

Are there more infos like this?

Jan

Isn't that the same as what we wrote ?

You have to think about how to split in different parts and add files to your project according to that, and give them proper names. About 20 or 30 files is no problem. But if you have 50 files, you could need a library.

I normally would make files like this: Menu, Menu_helper, Display, Keys, and so on.
The 'helper' files are usefull functions. Sometime I have also files with _glue, with functions to 'glue' the different parts together.

This is just normal programming. So I still don't know why there is a problem.

You can make a "new tab" in the IDE. Those tabs can contain .cpp and .h files. You can break up your project as much as you like into separate files. You can use classes. Just make sure that the main sketch (the .ino file) has an #include for each .h file so that the compiling system knows to include them in the building process.

At startup everithing has to be initialised in the rigth order and the correct timing.

Well then, make sure that, in the setup function, you initialize things in the right order.

Thanks for all the answers. Some of them are helpfull.

But to say "its normal programming, where is the problem? You will find the answers along the way. and so on ....." is not very helpfull.
If I had no problem I would not ask.

I know how to programm in more than 10 languages. Not all active. Modula2 or Pascal is no longer state of the art.

But I am new to Arduino. I like it. Things in Arduino are a little bit different. So even if I know how to organize Files in C++ or Java, I asked if there is a good tutorial for Arduino how to organize projects.

Now I have found what I have searched for.

Thanks
Jan

Things in Arduino are a little bit different.

No not really, at the end of the day it is just C++ so do it like any C++ project.

If you already know OOP, it's easy: two tabs per class, one .h one .cpp. One .ino tab only, the main sketch, Just remember to include all the .h files in the .ino too, even if they're already included in some class .h file.

If you already know OOP, it's easy: two tabs per class, one .h one .cpp.

Sure, but the key is to know that you can create additional tabs. That is not all that apparent from the crappy icons on the IDE. Nor is it obvious, or well documented, what creating new tabs means.

Of course, one would think that by the time the project has grown to the size where multiple tabs are going to be helpful, one would have become confident enough to experiment with all the buttons and menu items, and become familiar with the results.

Sure, but the key is to know that you can create additional tabs. That is not all that apparent from the crappy icons on the IDE.

Yes, that's a key point.

Is there somewhere a good source of how to organize larger Arduiono projects?

This is the concept of "project": you put all modules together. That approach requires coding for portability: you break down tasks into well defined and self contained modules. The interface is defined in .h files and the actual activities in the .c/.cpp files.

I just want to point out that you don't literally have to create the tabs. Close the sketch. Copy any extra .cpp and .h files into the sketch directory (alongside the .ino file). Re-open the sketch. All the new .cpp and .h files are automatically turned into tabs in the IDE (and considered part of the project). However to get them compiled you still have to #include any .h files from the main .ino file (not quite sure why as surely their presence in the directory should be enough?).

However to get them compiled you still have to #include any .h files from the main .ino file (not quite sure why as surely their presence in the directory should be enough?).

This is one of the (not so many) arduino-isms that one encounters when moving from simple one-timers to more structured code...

I just want to point out that you don't literally have to create the tabs. Close the sketch. Copy any extra .cpp and .h files into the sketch directory (alongside the .ino file). Re-open the sketch. All the new .cpp and .h files are automatically turned into tabs in the IDE (and considered part of the project).

The problem with this requirement, is that you cannot easily open a header file which is part of a library and see what is in it.

To look at libraries you need an external text editor. Virtually anything would do, although personally I use Crimson Editor on Windows, and Xcode on the Mac.