tips for longer program development

Hi
im after different ways to develop a longer program (longer by newbie standards), something similar in length to the GRBL program.

do you prefer to use the,

  • tabs,
  • completely different files for functions,
  • just write the whole thing out and code fold
  • other

which is easiest to track down problems etc…

Cheers

In a large program I would use different files for things that are conceptually separated.

if I write a class, then I'll have 2 files: one for the .h, one for the .cpp

If I need a set of generic helper functions, I'll write 2 files. a .h for defining the interface and a .cpp for the code.

If I need global variables across my files, I'll have a .h for the extern. Usually means inferior design though :slight_smile:

if I have a part of my code dealing with motors and another part dealing with artificial intelligence then they are different conceptually, so they would have their own files (assuming it's a bit long of course).

One driving factor for separating files into conceptual units is to encourage reusability. If I build something that stands by itself then I can reuse it. if it's in the middle of spaghetti code then you are toast... and designing for reuse is a good exercise, it means you need to think a bit more at an abstract level. What are the objects you are manipulating, how they are related or not etc... Forcing you to think about what you want to achieve before starting coding is a good practice for better code.

Design reuse makes it faster and cheaper to design and build new projects since the reused components will not only be already designed but also tested for reliability.

of course you need to balance that with code size and efficiency needs.

Hello again, thanks for your time.
by helper function do you mean like the array function you just showed me
arrays

sorry im not up with the C++ lingo yet.

do you have a folder with a lot of text files, where you store each function in each file, so you can just cut and paste said function into your current program?

would you consider the use of namespace to be innefficient or bad design.

In combination with what JML said, I tend to divide my code up into sets of classes each of which has a setup() and a loop(). A debounced pushbutton will be a class. A stepper motor that can seek to a location will be a class. All the loops are non-blocking, of course.

I “wire together” the components using constructors (usually).

Hi Paul,
what is your reasoning behind extra loops and setups, the program can only be in one place at one time cant it?

Have a look at Planning and Implementing a Program

I would always organize my code in that general format - small single purpose functions. If you prefer using classes you can do the same sort of thing within classes.

If there is a group of functions that belong together it would probably make the project easier to manage if they are put into a separate file. That could be a .ino file or a .h file, as it pleases you.

It probably makes more sense to put a bunch of functions into a separate file when you are satisfied that they work properly. Then you can call the functions from other parts of your program without concern for the detailed code in that file.

Personally I don't see any value in using classes unless the project requires several instances of the class. For example if I was writing a program to simulate bee-keeping I would have a class for "bee" (because there are lots of them) and for "hive" because there will probably be several of them. But I don't see any value in having a class for "farm" as there will only be one of them.

...R

Additionally, if you're 'serious' (!?) about programming, you need a 'better' IDE than the standard one. By 'better' I mean one that gives contextual help, code completion, parameter suggestions and so on, whilst allowing you to organise the code better too.

I use the JAVA based Eclipse IDE for which there is a separate Arduino add-on (free, but he likes a donation). This makes it more-or-less like Visual Studio.

That said, I'm guessing from your comment

sorry im not up with the C++ lingo yet.

that you're not a developer, per se, so you may find it hard going to set up. Although I think it better than sliced bread, I also use Visual Studio every day at work so what might be obvious to me will most certainly not be obvious to non-developers - but, you never know, if you follow the step-by-almost-step instructions you may think 'Hey, this is great'. It took me two installation attempts to get it right and a good 3 hours - and then about a month to get it fully tweaked!

You can see how I use it just by watching any of my more recent YouTube videos but I don't really mention it because I know that most Arduino developers will never use it - it just helps me demo my code and allows multiple connections to the Serial Monitor window (up to five) so you can monitor more than one Arduino process - a godsend in my recent (not yet published so don't go looking) 433Mhz transmitter/receiver wireless cat-run rain-detector project which requires two modules (transmitter/receiver).

More details here: http://eclipse.baeyens.it/

And the Arduino plug in: GitHub - Sloeber/arduino-eclipse-plugin: A plugin to make programming the arduino in eclipse easy

Even if you don't want to try this others reading this might be tempted!

This is supposed to be an image (from dropbox) - you can right-click and open image in new tab:

I also found the Arduino IDE unsatisfactory after the basics. Also, I want to do all my programming using the same editor so I don't (at my age) have to remember different ways of doing the same thing.

However I got tired of Eclipse and its variants years ago because I prefer a simple system. I use the Geany editor to create my code and I have a wriiten a short Python program so I can use the Arduino IDE from the command line to compile and upload programs from within Geany.

...R

travis_farmer:
what may I ask are the command-line options to compile and upload?

My Python code is here. This is the RTFM

It works with 1.6.3 also. I have not tried a newer version.

...R

travis_farmer:
I suppose RTFM was called for, I wasn't thinking.

I use the term in jest - I did not mean any criticism.

In this case it would not be easy to find if you did not know it existed.

...R

cheers for the replies.
i agree arduino ide is good for the basics, i use it sometimes to try out a new function seperate from what i am working on, and then put it into my program.

i had a look at a lot of ides, i ended up with the arduino ide and VS7. setting that up was a fun day job. it has a few install bugs in it (stalling). getting the install order order right was the hardest thing.

ultimately i chose VS7 because of the hardware debugging, some of the handy feautes ive found so far are setting up keyboard shortcuts and reference renaming, and definately the colour differentiation.

No im not a developer (boilermaker, mechanical draftsman), 5 weeks so far with c++. I did have a crack a year ago, but it was all to confusing. this time i stuck at it. if it wasnt for Arduino and the forum there is no way i would have stuck at it this time.

the whole reason for my question is that by far the hardest part to learn for me has been getting access to 'data' from different parts of the program. so i want to learn a solid technique right from the start. learning how functions/methods work was easy enough, but joining them all together seemed (still) a bit cryptic.

thanks robyn

But I don't see any value in having a class for "farm" as there will only be one of them.

what im working on at the moment, i just started writing a class (mostly because i want classes to become automatic in my head), when it was blindingly obvious reading that there was no need.

joeblogs:
Hello again, thanks for your time.
by helper function do you mean like the array function you just showed me
arrays

sorry im not up with the C++ lingo yet.

do you have a folder with a lot of text files, where you store each function in each file, so you can just cut and paste said function into your current program?

would you consider the use of namespace to be innefficient or bad design.

A “helper function” isn’t really a standard part of the lingo. I assume what is meant is a more generic function that can have broad use beyond your current sketch.

It’s been a while since I’ve gotten back into microcontroller projects, but for my desktop C++ developement I have a certain core of my own custom functions I’ve made that can be included into any project. One example is my in_window function that tests if a value is in between two limits; it return 1 if above the window, 0 if within, and -1 if it is below the window. It is in a math_functions.cpp file that is outside of all my other projects, and all the projects refer to that one file. Any change I make to that file don’t have to be copy+pasted to all the other projects since they all refer to the one master copy instead of their own local copies.

// Checks if value is between the limits, inclusive.
// return 1 : above the window;
// return 0 : within the window;
// return -1: below the window;
int64_t in_window(int64_t value, int64_t limit1, int64_t limit2)
{
	
    auto high_limit = std::max(limit1, limit2);
    auto low_limit = std::min(limit1, limit2);

    if( value > high_limit )
        return 1;
    if( value < low_limit )
        return -1;
    return 0;
}

For Arduino, you wouldn’t want to copy+paste this into all your sketches. You can make your own library just like the EEPROM.h or Serial.h libraries that all your sketches will be able to use.

joeblogs:
the whole reason for my question is that by far the hardest part to learn for me has been getting access to 'data' from different parts of the program. so i want to learn a solid technique right from the start. learning how functions/methods work was easy enough, but joining them all together seemed (still) a bit cryptic.

And I have a series of YouTube videos aimed at beginners that explore various components and techniques that will probably assist you. It covers lots of things but many items are driven from the questions that are repeatedly asked here on the forum.

URL in the footer of this post, subscribe if you think it worthwhile :slight_smile:

Cheers Jiggy,
so you dont need a header file as such, or create class/libraries. You can call a cpp file full of functions.
do you call it the same way and set it up the same as an instance of a class.

I don't know about .cpp files but I have just put stuff into .h files (without corresponding .cpp files) and then used #include "myFile.h"

...R

For any project beyond a simple "hello world" or "blinky led" that is to be maintained over time, using a source code manager is an absolute must.

This will allow you to track your changes and see what has changed between your "checkpoints".
I'd highly recommend using git as you can create a local repository and if/when you are ready, you can push the updates to a cloude repository (github, bitbuket, etc...). This can be important even if you don't ever intend to share the s/w as the cloud repository can act as your backup.

To make it easy to use, I'd recommend using a git package that integrates directly into your GUI file manager.
That way at a glance you can see all the files that have changed and click on any file and see the changes you have made.

rabbitvcs is great on linux
if you happen to be using windows, then have a look at tortoiseGit

--- bill

Hey bill,
struggling to get my head around coding, i hear what your saying about file managment. At the risk of costing myself somemore work later im not going to confuse my self with git just yet.

J-M-L, or any one who sets up like below

if youre around im struggling to get this to work

If I need a set of generic helper functions, I'll write 2 files. a .h for defining the interface and a .cpp for the code.

as far as i can tell you can only put variables in the cpp and function (prototyes)? in the .h file

any chance you could show a simple example.
ive tried everything i can think of and i know the answer is simple, but it escapes me.

Ive setup 1 main INO file (all in VS7), 1 .h file and 1 cpp file but im struggling to move variable values from the cpp to the ino.

ive found doing a class easy enough in other programs, but setting it up with .h and .cpp seems to work a lot different

ive even tryed namespace to pull global? cpp variables to the ino page but cant get it to work

The compiler compiles a *.cpp module file only once, but header files can be #included into many modules. I'm not sure right now about the handling of multiple declarations in C++, but it may be required to mark variable definitions (in the header files) as "external", and have one variable declaration of that name in only one *.cpp file.

To make a variable in a *.cpp file known in the *.ino file, its declaration must reside in a header file that is #included in the *.ino file. The Arduino IDE does some magic behind the scene, e.g. includes the header files of all libraries, which have been added to the project. VS most probably will behave differently.

hmm
going to have to have a play, in both VS and .cc IDE, VS doesnt seem to want me to declare any variables in the header.

sorted i think,
VS7 was automatically putting #include "file.h" in the file.cpp so the compiler was seing it as 2 identical things