Including Library just sometimes

I wrote a code that can be used 2 ways

For one I need the Servo Library For one the Servo library mustn't be included

since both will access Pin9 and the servo Library blocks PWM on Pin9 when included. I need to define a variable telling my program how to output anyway.

So is there a way to include a library dependent on what this variable was declared to?

For example

int UseServo = 0; // 0 or 1

To make the compiler only include Servo.h when UseServo is 1 ???? Or do I need to comment / uncomment that each time manually?

#ifdefor#if could be what you're looking for

Thanks, exactly what I was looking for!

Ok it’s doing what is expected but now I got another problem…

When I use the Servo library I need to create a Servo Object.
When I do this with normal if() before the setup routine (where it needs to be regarding examples) it doustnt accept the normal if() there.

When I use

#if (UseServo == 1)
#include <Servo.h>
Servo MotorRegler;
#endif

it doesn’t seem to be recognized at all.

try like this:

#define USING_SERVO true

#if USING_SERVO
#include<Servo.h>
#endif

#define USESERVO

#ifdef USESERVO
#include <servo.h>
#endif

And every where you need the servo you need to do something similar with the #ifdef

And every where you need the servo you need to do something similar with the #ifdef

I don't understand that one ?

So it doesn't work using if at all but wen using ifdef it does?

Gorkde: I don't understand that one ?

So it doesn't work using if at all but wen using ifdef it does?

no, you have to address any/every place in your code that uses (in this case) Servo objects...

everywhere you use Servo, you have to manage with the #if direcctive

e.g.

#if USING_SERVO
  Servo1.write(180);
#endif

I now did:

#define UseServo 0

#ifdef UseServo 
#include <Servo.h>
Servo MotorRegler;
#endif

It compiles fine. Does it work that way as well?
I wanted to use the 0 or 1 I define to later in my Sketch do something like:

if (UseServo == 0)
  {
    DO SOMETHING
  }
  else if (UseServo == 1)
  {
    DO SOMETHING ELSE
  }

if you are going to use pre-processor directives, you should just learn how they are used....

read about them, see how they are used in code and when you've done that, test it by compiling your code.

test it by compiling your code.

And running the code.

PaulS: And running the code.

Sorry, right.

Whacking a stick around in the dark and asking folks if you've hit anything isn't a learning strategy.

I already looked it up but did find many confusing pages…
Ok now after rethinking I saw the problem…

My goal is to avoid defines later in the code.

My new ideas:

#define ServoON

#define UseServo 0
#ifdef ServoON 
#define UseServo 1
#include <Servo.h>
Servo MotorRegler;
#endif

(commenting or uncommenting the first line)

I also had this idea:

#define ServoON

#ifdef ServoON
#define UseServo 1
#include <Servo.h>
Servo MotorRegler;
#else
#define UseServo 0
#endif

(commenting or uncommenting the first line)

Gorkde:
My goal is to avoid defines later in the code.

you cannot avoid it

you can minimize it by using more functional programming…

#define USING_SERVOS true

#if USING_SERVOS
#include <Servo.h>
Servo myServo;
#endif

void setup() 
{
  Serial.begin(9600);
  if(setupServos())
  {
    Serial.println(F("Servos Included"));
  }
  else
  {
    Serial.println(F("Servos NOT Included"));
  }
}

void loop() 
{
  doServoStuff();
}

bool setupServos()
{
#if USING_SERVOS
  myServo.attach(9);
  myServo.write(0);
  return true;
#endif
  return false;
}

void doServoStuff()
{
#if USING_SERVOS
  static uint32_t lastMillis = 0;
  static bool toggle = false;
  if(millis() - lastMillis > 3000)
  {
    toggle = !toggle;
    myServo.write(toggle? 180 : 0);
    Serial.print(F("moving Servo to "));
    Serial.println(toggle? "180" : "0");
    lastMillis = millis();
  }
#endif
}

but you cannot reference objects (Servo in this example) without defining them (and including the class definition).

You can't avoid subsequent #ifdef...#endif, but you can mitigate them by defining macros

#ifdef WITH_SERVO
  ATTACH_SERVO(x, pin)   x.attach(pin)
  WRITE_SERVO(x, val)   x.write(val)

#else
  ATTACH_SERVO(x, pin)
  WRITE_SERVO(x, val)

#endif

but it can get tedious for classes with lots of methods

You can't avoid subsequent #ifdef

When I define something it (for my knowledge) repaces every ocuurence in the code with what I defined. So before compiling he replaces "UseServo " with "1" or "0" in the code, then compiles.

So a normal if in my code for example:

if (UseServo == 1) or if (UseServo == 0)

Would translate to

if (1 == 1) or if (0 == 1)

before compiling. Already tested it and it seems to work fine.

This is what you said you needed:

Gorkde: For one I need the Servo Library For one the Servo library mustn't be included

This is your solution:

Gorkde: When I define something it (for my knowledge) repaces every ocuurence in the code with what I defined. So before compiling he replaces "UseServo " with "1" or "0" in the code, then compiles.

So a normal if in my code for example:

if (UseServo == 1) or if (UseServo == 0)

Would translate to

if (1 == 1) or if (0 == 1)

before compiling. Already tested it and it seems to work fine.

so if you use this method (and no one has seen your working code) you'd still need to include the library and create the object, lest you get compile-time errors whenever class objects are referenced (yes, even if they are inside a conditional statement).