Is it possible to "include" a block of code between sketches

I have a block of code that I use in various sketches. I want to modify the code in one sketch and then have the other sketches "pull" that code in during compiling. I understand that the statements go at the beginning of a sketch. The code I want to "pull" is used at different locations in different sketches.

What I have been doing is changing the code after each modification and pasting into the appropriate sketches which is prone to create problems if I forget to update the code before compiling.

that's what libraries are for

Welcome to the forum

If the block(s) of code are in functions then if you put them in a .h file you can use them as a library and #include the library in any number of sketches

So, are the blocks of code functions or are they code not in functions ?

sounds like you have a common function used in various programs

i have function that i used for debugging and other things that reads commands from the serial monitor. it is in a separate file (happens to be .cpp) i copy the file into the directory with a sketch and add a call to it in loop() (in my case). i also add an extern reference to it in the .ino file

I do the same for functions and blocks of complete code. What I am referring is to use a block of incomplete code that the compiler would insert at a given location during compile time. These code segments cannot be made into functions since some times they would refer to variables that are not in the global scope.

I even have on occasion to use the same char arrays in different sketches that I modify over time. This "code segment" would be inserted into a given function or code block.

I am looking into the Visual Studio to determine if it can handle the insertion of code blocks.

now i'm curious. would you mind posting the code

i'll bet that code snippets could be made more reusable

If you do not want to have multiple copies of your library in every sketch directory using it,
rather add an extra entry to your libraries directory
Easiest have a look at the structure of other libraries there. And read

This might help as well: First library troubleshooting

Use #include

Yes, they can. The functions need to be written better. Instead of accessing the variables directly, they should be passed as parameters, by value. If the function needs to update the variable, it should be passed by reference instead of by value.

Then how will the block of code be able to refer to those variables ?

Please post an example of the type of code block that you would like to insert

Variable declarations do not have to be in a function

You can of course create a function returning a pointer to such static text, if this is really what you want. Or you can even add variables and constants to your libraries.

This is possible, but I would not recommend it.

For the sake of completeness, you could:

  • Create a file, named something like myBlock.h and put the block of code in there.
  • Use #include "myBlock.h" whenever you need that particular block of code.

E.g.,

// myBlock.h
Serial.println("Hi.");
// sketch.ino
void setup() {
  Serial.begin(9600);
#include "myBlock.h"
#include "myBlock.h"
#include "myBlock.h"
}

// ...

Again, this is not an advise. This technique can potentially lead to lots of problems, especially when the code is included multiple times. You are probably better off writing proper functions and creating a proper library.

1 Like

Where would myBlock.inl be located in relation to the sketch ?

Good point.

It should be located in the same folder as the sketch. Also, the build system apparently does not like including files with the extention .inl, I have changed the example accordingly.

But @allthesame wants to use the contents of the file in several sketches so having it in the sketch folder is not convenient, hence the suggestion to make it into a library

This seems to be working. I just incorrectly thought the include statements had to be at begin of sketch. But placed anywhere seems to cause the "xxxx.h" file contents to be included at the location of the include statement.

I have included the path to the locations of these code snippets and variables and this is working as I requested. Thanks jfjlaros for the push I needed to try a built-in feature.

As a matter of interest, where exactly have you put the "xxxx.h" file ?

I put them in a separate folder in my sketches folder. I am moving all the snippets that are under modifcation to this location and adding include statements in the primary sketches.

I think I see use for this also to "store" passwords, account names, code not wanting to share , etc. in separate files from the sketch. If I share a sketch I do not need to xxxxx-out that info since they would not be stored in the sketch.

So in other words you are using a library

Yes. I was just under impression that include statement had to be "complete" code rather than any text that the compiler would insert at that location.

This works great for a large amount of html code that I currently store directly in the sketch and that I have to scroll past to find the code I am modifying.

Understand that arduino IDE is not my primary skill set. I am installing ESPs around my various buildings to communicate back to a security system door and window status, allow control of garage doors remotely, notify status change, building temperature and humidity, etc. The main ESP32 server has about 1000 lines of code and servess as data repository, security system updating, and webserver for all the clients and displays.

Putting variable definitions and function implementations in an #include file is a hack. As noted by @jfjlaros, you will run into linking errors if the file is #include(d) in multiple translation units. To do it correctly, the #include file should only contain 'extern' variable declarations and function prototypes. The variable definitions and function implementations should go in a separate .cpp file.