1

I'm looking for the Arduino/C++ equivalent of COBOL's 'copy' statement.

I'm working on a program that is approaching 600 lines of code in the TopLevel.cpp source file. This is hard to manage because I feel like I spend all my time scrolling up and down the source file. The code in question does not easily lend itself to conversion to a class because it is sections of loop/main. I've tried using xxx.h (for storage definitions) and yyy.cpp (for code sections). Those approaches yield error messages, which I think are due to the different timings of the Arduino pre-processor and the compiler code that resolve the #include statements.

Thanks for the help. --CVTBrakeman

p.s. I wrote my first program in 1967; the language was GOTRAN on an IBM 1620. For almost 50 years, I made my living designing/writing COBOL and SAS code on IBM mainframes. Yes, Y2K was real and I survived it.

4
  • you can have many .ino files in an Arduino project. Use Add file in Sketch menu in IDE. what error messages for h/cpp? – Juraj Dec 1 '19 at 19:51
  • You can use a #include directive. You can build your C++ subclasses in separate .CPP & .H/.HPP files. The Arduino IDE is just a wrapper around a C++ compiler and a bunch of scripts to load the .HEX to the Atmel chip using ICSP. – Dougie Dec 1 '19 at 20:00
  • Thanks for the help; I think the solution is to restructure the program and convert my 'include' code to separate classes. Probably should have done that in the beginning. It looks to me as if included files are compiled separately. Variables defined in the included code are no longer visible to the setup/main code blocks. Thanks for your help. – Andy Arnold Dec 1 '19 at 20:34
2

This is more like a general programming question than an Arduino question but, anyway...

You don't need to write classes to sort your code into files. You just need to think in term of independent pieces of functionality. Each of those pieces would be implemented in a .cpp file, with the interface declared in a .h file.

As an example, let's say your Arduino is controlling an hyperdrive. Managing the dilithium plasma generator is one of those functionalities that can be separated in its own files. The .h files declares everything that is needed for using that functionality:

plasma_generator.h:

// Read the state of the plasma.
float plasma_density();
float plasma_temperature();

// Control the plasma.
void set_plasma_target_temperature(float target_temperature);
void ignite_plasma();
void turn_off_plasma();
void add_dilithium(float amount);

Then the .cpp file contains the implementation:

plasma_generator.cpp:

#include "plasma_generator.h"

// A "static" function is a function private to this module.
static float get_gauge_reading(int gauge_number)
{
    ...
}

// Implementation of a public function, described in the .h file.
float plasma_density()
{
    float raw_reading = get_gauge_reading(42);
    ...
}

...

In your main program (or even in other modules, if needed) you #include the .h file and use the functionality defined therein.

1

There are two ways of doing this:

  1. Multiple INO files

The Arduino IDE combines multiple INO files into one single CPP file before compilation. You can split your program across multiple INO files at will. It makes it easier to manage, since you can split your program into logical chunks at will, but you don't have to think much beyond "which file shall I stick this function in?"

  1. Create libraries

Some functionality of your program could be placed into a library or multiple libraries. This is kind of like option 1, but you have to think more about C++ syntax and suchlike that with INO files (function ordering, externs, etc). It does have the benefit that you could easily re-use code between programs, though

Personally I usually use option 2 before option 1. If a code is genericisable ("able to be made generic") then it can be made into a library - after all, I may want it later for some other program. However, if all you want to do is split your program into different chunks with each chunk a different topic or area of your program then option 1 is definitely for you.

Alternatively you might consider a different IDE, such as UECIDE which gives you such things as function bookmarks& to make navigation of your code easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.