I have several variations of a sketch.
I started with a basic sketch and added options.
So I have a copy of each step with different options.
It is becoming a hassle to maintain all of them.
I would now like to amalgamate all the options into a single sketch and invoke the desired runtime options with a variable.
The problem I am facing is that some options require different libraries.
I am therefore trying to include libraries on an optional basis.
Something like this...
int optSD = 1; // 0 = No, 1 = Yes.
if (optSD == 1){
#include <SD.h>
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
The above method does not compile.
sketch_aug16c:3: error: expected unqualified-id before 'if'
expected unqualified-id before 'if'
My research shows I cannot have "free standing code" where I have it and need to wrap it in a function.
I moved the if statement into the void loop but it still does not work.
Unfortunately, these conditional includes never worked with the Arduino IDE. Any lines starting with #include are processed and the file is always included...
guix:
Unfortunately, these conditional includes never worked with the Arduino IDE. Any lines starting with #include are processed and the file is always included...
Hmm, you are correct.
#define sd_SD
#ifdef sd_SD
#include <SD.h>
#include <SPI.h>
#endif
void setup()
{
// Start Serial
Serial.begin(9600);
while (!Serial) {;} // wait till port connects
Serial.println(F("Serial started.")); // test print
#ifdef sd_SD
Serial.println(F("SD Card Available.")); // test print
#else
Serial.println(F("No SD Card Available.")); // test print
#endif
}
void loop()
{
}
Defined result.
Sketch uses 6,252 bytes (19%) of program storage space. Maximum is 32,256 bytes.
Global variables use 818 bytes (39%) of dynamic memory, leaving 1,230 bytes for local variables. Maximum is 2,048 bytes.
// #define sd_SD
#ifdef sd_SD
#include <SD.h>
#include <SPI.h>
#endif
void setup()
{
// Start Serial
Serial.begin(9600);
while (!Serial) {;} // wait till port connects
Serial.println(F("Serial started.")); // test print
#ifdef sd_SD
Serial.println(F("SD Card Available.")); // test print
#else
Serial.println(F("No SD Card Available.")); // test print
#endif
}
void loop()
{
}
Not defined result.
Sketch uses 6,254 bytes (19%) of program storage space. Maximum is 32,256 bytes.
Global variables use 818 bytes (39%) of dynamic memory, leaving 1,230 bytes for local variables. Maximum is 2,048 bytes.
However, since I can use #ifdef / #ifndef in the top of the sketch, that should allow me set conditions for everything else except the libraries themselves, which I could do manually.
guix:
Unfortunately, these conditional includes never worked with the Arduino IDE. Any lines starting with #include are processed and the file is always included...
Oh dear, but true now that I have tested it in the IDE....
guix:
Unfortunately, these conditional includes never worked with the Arduino IDE. Any lines starting with #include are processed and the file is always included...
If that is true, then the IDE needs to be fixed. This behaviour is simply wrong.
My current solution is to manually edit a series of "defines" and "includes".
I would like to implement a work around by using 2 sketches similar to the suggestions in Post #5.
I have tried this, and it did not work for me - but maybe I did something wrong.
So is this possible...?
I would like to have my main sketch with the defines listed and the full sketch - but no libraries.
From the the main sketch I will include a separate file say "librarylist".
Librarylist will contain the conditional includes.
So main sketch will look something like this:
define lan_ethernet;
include <librarylist.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
I have advanced and placed librarylist.h in a folder under libraries.
No problem to include librarylist.h, but the IDE does not find optional libraries automatically.
The code compiles if I place the libraries directly in the librarylist folder.
However, I want to place the libraries in subfolders under librarylist.h
How does one include a library and specify a path to the library?
It was the "completePath" I was having trouble with initially.
I since figured that, but that is only part of the problem because even if u supply the complete path, it won't compile because u run into linker problems.
Seems there is no way to do it except place all the library files in the same folder as the referring library.
This could also be a problem because some libraries have sub folders with the same names, so u will have conflicts.
I have given up for now.
Alternative is to migrate to another IDE, which is on the cards but I need more to build up more experience.