Including a header file depending on the state of a button

Hello
I am using an Atmega328 (UNO) and use already Timer0 and Timer1(Servo) in my script.
I want to use also the tone-library and the IrRemote-library.
Both use the Timer2, so its not possible together (I dont want to use the TimerFreeTone-library).
Both, Tone and IrRemote have not to be used at the same time.
So I want, that it is possible to choose at the start of the program (Reset) depending on the state of a button, that either the tone-library or the IrRemote-library will be included.
But I dont know, how.

I think, this would work, but its not dependig on a button state:

#define buttonstate 1

#if (buttonstate)
#include "tone.h"
#endif

#if (buttonstate==0)
#include "IrRemote.h"
#endif

But how can I read a button state before including header files?

Or is it possible, to include header files within a running programm - means during loop?

Thank you for your help
Best regards
Peter

You can't do it. Preprocessor directives like #include are processed before the code is even compiled and uploaded. Button state is read at run time. So there's no way for the button state to be known before including those files.

I'd look to see if there's an alternative library that uses a different timer (would start with tone).

No this isn't possible. Including header files is something that is done at the time the code is compiled, not when it is running. You can't decide what code to include at runtime.

You can include both libraries and decide which one you will call code from at runtime. But you can't decide to include one and not the other at runtime.

Hi Peter. No, your idea will not work, I'm sorry.

Can you explain what the project/circuit is for? Maybe we can suggest another approach.

Thank you for the fast response.

Ok, the issue with the including header is not possible.

Regarding the last hint with deciding during the run.
Because both libraries use the same timer, unfortunately I get a compiling error.

Or did I understand it in the wrong way?

About the project:
Its a robot vehicle, which uses Servos(Timer1).
Among other sensors I want to use a buzzer (Tone) and the IrRemote to communicate to another robot.
But as I mentioned, it would be also ok for me, if both Tone and IR can be decided to be taken by setting a switch.

Thanks
Claus

Hello again

Using another timer (changing the tone library) does not help, because I use all other timers.
I am going to use the TimerFreeTone library. The disadvantage is, because of using no timer, the programm stops during playing the melody.
But at least I can use the piezo without conflicts to other timers.

Thank you
Best
Peter

If your project will work with a single sound frequency, you can get active buzzers that have the frequency generation circuit built in. So you only need to supply power. This means you can control the buzzer with only digitalWrite().

You can get 16-channel PWM/servo driver boards very cheaply on eBay etc which use the PCA9685 chip. These will be able to drive your servos (also fade LEDs, drive DC motors, provide extra digital output pins etc). The chip has its own timers built-in, so does not need to use the Arduino's timer.
s-l300 (25).jpg

s-l300 (25).jpg

Yes, I want to have different frequencies.

Thank you for the hint with the PCA9685.
Perhaps I use this chip for my purpose.

Best
Peter

You CAN include all libraryes you want.
You CAN'T include a library if a button is pressed.
You CAN erite a program that does different works if a button is pressed.
You CAN also remmeber if the button was pressed at the biginning and use this information like button pressed

Silente:
You CAN include all libraryes you want.

And pray that it compiles ::slight_smile: