Hello Friends,
I have created configuration.h file, where I want to run or don't run some functions in the sketch. For example #define motor_direction 0 // if 0 clockwise, if 1 counterclockwise.
I can do this way:
if(motor_direction == 0)
clockwise();
if(motor_direction == 1)
counterclockwise();
But I believe it must be something like this #if def motor_direction = 0
clockwise(); #endif
No. The compiler evaluates all constant conditions at compile time and omits all false branches. IMO this happens already at the lowest optimization level.
Normally the benefit of using an enumeration instead of a #define is that you get compiler checking of types and it reduces the errors you could make. In this case you don't get that because the parameter type to analogRead(x) is an int, but it makes for more readable code.
Edit your post using the in the tool bar just below your post
select the code part and press the <code/> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)
// comment out the one you don't want
//#define stop STOPPWM
#define stop STOP
#if !defined(stop)
#error You need to pick one
#endif
Now the question is it correct way if I add more defines? For example: #define turn_left SOFT_START #define turn_left NORMAL_START
Should I just add in define.h like this?
//#define turn_left SOFT_START
#define turn_left NORMAL_START
#if !defined(turn_left)
#error You need to pick one
#endif
// comment out the one you don't want
//#define stop STOPPWM
#define stop STOP
#if !defined(stop)
#error You need to pick one
#endif
Thank you very much! This define option allows me to change things in my code very fast and easy! I am really very happy!