3

I'm wondering if there is any way to include the text of a file into a sketch by using a single placeholder command, similar to the "#Include" command when using Server Side Includes on .shtml files.

I'm sorry if I'm not wording this correctly. It was hard for me to phrase it and also hard for me to search about it, since the nouns seem so generic.

I understand that some parts of code can be separated out into their own functions, and that those functions could be saved in a different .ino file. But I'm wondering about for the cases that can't be separated out into functions (like for declarations at the beginning of a sketch, or an area in loop() that would require too many variables to be passed to the function).


EDIT: I think I should've been more specific: In one example, I have lots of html code being served from the loop() function. The Arduino webserver is serving a complex page that shows about 100 buttons on the webpage. I would like to separate the page-serving code to a separate .ino file and have it opened as a separate tab in the Arduino IDE. That way it's easy for me to open that tab whenever I want to modify the webpage, but also the code for serving the page won't take up so much space in my main file. The code isn't completely html (it has lots of functions and conditionals), so that's why I can't just save it as an external .html file and have it served from that file.

As another example: At the top of my sketch I have lots of defines for wireless signals that need to be sent. They look like this:

const unsigned long a1o = 4216115, a1c = 4216124, a2o = 4216259, a2c = 4216268, a3o = 4216579, a3c = 4216588, a4o = 4218115, a4c = 4218124, a5o = 4224259, a5c = 4224268; //remote control 1

There are 10 lines of code that look like that, and I never have to modify them because they're all constants. When I'm searching for bugs in my code, I never have to refer to those lines, and they're usually just in my way.

  • 2
    Yes. It's called #include. – Majenko Nov 23 '15 at 12:37
  • 1
    cases that can't be separated out into functions. This sounds very wrong. If you can't break you code into smaller chuck, creating separate files isn't going to much, and will probably make your code even harder to understand. – Gerben Nov 23 '15 at 13:12
  • In those -few- cases where there are really complex sets of parameters, the usual solution is to pass them through a struct. – Igor Stoppa Nov 23 '15 at 13:30
  • Or you can use global variables... Or just use the include (remember to call your file "xxx.h" instead of "xxx.ino", otherwise the compiler will try to compile it twice, thus giving errors) – frarugi87 Nov 23 '15 at 13:42
  • 1
    You should also use PROGMEM, otherwise those variables also end up in RAM. See Putting constant data into program memory (PROGMEM) for details. – Nick Gammon Feb 22 '16 at 20:21
1

As suggested in comments, you can use #include statements to include texts of files into a sketch. For example, suppose files c1.h, c2.h, c3.h, c4.h, and c5.h are in directory ~/sketchbook/tincludes, along with the file tincludes.ino, which is as follows:

#include <Streaming.h>
void setup() {
  const long int
#include "c1.h"
#include "c2.h"
#include "c3.h"
#include "c4.h"
#include "c5.h"
 etc=5   ;
  Serial.begin(115200);
  Serial << z1o <<  z1c <<  z2o <<  z2c <<  z3o <<  z3c <<  z4o <<  z4c <<  z5o <<  z5c;
}

Further, suppose c1.h ... c5.h each contain one line of code, as follows:

==> c1.h <==
  z1o=25829, z1c=9774,

==> c2.h <==
  z2o=7850, z2c=25585,

==> c3.h <==
  z3o=7103, z3c=17981,

==> c4.h <==
  z4o=30827, z4c=24511,

==> c5.h <==
  z5o=4167, z5c=10036,

Then the sketch will compile ok, and the sketch-lines with const long int through etc=5 ; will compile the same as if they were replaced by

const long int z1o=25829, z1c=9774, z2o=7850, z2c=25585, z3o=7103,
     z3c=17981, z4o=30827, z4c=24511, z5o=4167, z5c=10036, etc=5;
  • This will work only if you include the h files just once. As soon as you include them in two cpp/ino files you will get linker errors. The usual way is to split definition and declaration in two files and use extern to notify the linker to search for them somewhere else. Check my answer for what i think is the correct way to deal with shared variables – frarugi87 Nov 24 '15 at 9:30
1

Usually you should not define a variable inside a header file. If, for example, you write

// file myvars.h
int var_a = 10;

// file main.ino
#include "myvars.h"

// file another.cpp
#include "myvars.h"

the linker will fail, because there are two definitions of the same variable var_a.

The correct way to do it is to declare a variable as extern inside the header file, then define them into a cpp (or ino) file just once. I mean

// file myvars.h
extern int var_a;

// file myvars.cpp
int var_a = 10;

// file main.ino
#include "myvars.h"

// file another.cpp
#include "myvars.h"

This way you will be able to access var_a from both main.ino and another.cpp. If you don't want to add another cpp file for the definitions, you just have to provide the definition inside ONE of the ino or cpp files you already have.

So, in your case, just make two more files:

file remote_control_1.h:

extern const unsigned long a1o, a1c, a2o, a2c, a3o, a3c, a4o, a4c, a5o, a5c;

file remote_control_1.cpp:

#include "remote_control_1.h"
const unsigned long a1o = 4216115;
const unsigned long a1c = 4216124;
const unsigned long a2o = 4216259;
const unsigned long a2c = 4216268;
const unsigned long a3o = 4216579;
const unsigned long a3c = 4216588;
const unsigned long a4o = 4218115;
const unsigned long a4c = 4218124;
const unsigned long a5o = 4224259;
const unsigned long a5c = 4224268;

and in your files just write #include "remote_control_1.h" to access those variables

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.