include file only if it exists

Is there any way to include a file only if it exists?

I want to publish a project of mine to github.
But there are some values defined that can change sometimes but are not critical to the projects source code.

For example the number of leds defined at the beginning of the code can change depending on which hardware I am using.
I think it won't look good if the commit for "fixed typo" would include a change for this value.

Another scenario would be a password that should not be exposed in the git repository.

So I think I would need something like this:

if ( exist(custom_values.h) ) {
  #include "custom_values.h"
} else {
  #include "default_values.h"
}

I could ".gitignore" custom_values.h and the sketch would still run for people who clone this repository.

But maybe there is a better approach to my problem.
I am just a beginner with arduino and git. ::slight_smile:

Usually not done in the code. Cf this question

Ok.
Using a separate file to define wich files should be included and generating this file with a extra script is a way.
But it is not a nice way. :slight_smile:

How do other users handle this?
I can't be the first one.

Probably need to explain what you are trying to achieve - class templates might solve your needs

Like I said in the first post.

One scenario is where I have sensitive data like passwords in my sketch, but I want to share this sketch with the public (via github).

Another example is a value (amount of leds on a stripe) that changes often, because I am developing on different devices.
But I don't want these changes to be commited to my repository.

Password can be passed as parameter during class instantiation and number of LEDs could be part of the template

Thanks.
I will look up "class instantiation" and "the template" in the next days. :slight_smile:

Go have a look at the fastLED library for example

To declare a strip of LEDs you do something like

#include "FastLED.h"
CRGB leds1[30];
CRGB leds2[60];
CLEDController *controller1;
CLEDController *controller2;

void setup() { 
  controller1 = &FastLED.addLeds<WS2812,2>(leds1, 30);
  controller2 = &FastLED.addLeds<WS2812,10>(leds2, 60);
}
FastLED.addLeds[color=green]<WS2812,2>[/color][color=blue](leds1, 30)[/color]

This call instantiates (create an object of type FASTLED) and the green part helps configure the library in one way (type of LED strip and pin where it's connected) while the blue parts are arguments you pass to the instance to help set it up