Trying to understand #define

I'm confused by #define, #ifdef, #endif

My understanding is that if I put:
#define DISPLAY
at the top of a sketch, and lower down in the sketch put:
#ifdef DISPLAY
some code
#endif
then the some code is compiled into the .hex and if I remove the initial definition, or comment it out then the some code is skipped.

The weird thing is that in the same sketch I am working with I have one set of these #defines which work and a second set which still compiles in the code despite the #define being commented out.

What might be going on?

We can't help if you don't post "the same sketch" you talk about.

Hi,

FYI:

Perhaps the name you chose was previously defined by one of the many libraries.

Or you wrote something wrong and we can't tell you what because we can't see your code.

Your understanding of what is the behavior, is correct - so if code is generated it means the define exists. Note that you can also associate a value to a define « variable » so there is a difference between #if and #ifdef

➜ long story short post a code example of your claim.

here's an example

#undef  MyHW
#ifdef MyHW
const byte pin = 13;
#else
const byte pin = 0;
#endif

int           dutyCyle = 30;
unsigned long Period = 5000;
unsigned long onPeriod  = Period * dutyCyle / 100.0;
unsigned long offPeriod = Period - onPeriod;
unsigned long period;

unsigned long usecLst;

byte state;

void softPWM (
    byte pin )
{
#ifdef MyHW
    unsigned long usec = millis ();
#else
    unsigned long usec = micros ();
#endif

    if ( (usec - usecLst) >= period)  {
        Serial.println ("timeout");
        usecLst = usec;

        if (LOW == state)
            period = onPeriod;
        else
            period = offPeriod;

        state = ! state;
        digitalWrite (pin, state);
    }
}

void loop ()
{
    softPWM (pin);
}

void setup() {
    Serial.begin (9600);

    state = LOW;
    digitalWrite (pin, state);
    pinMode      (pin, OUTPUT);
}

https://en.cppreference.com/w/cpp/preprocessor/conditional

Seems OP is missing in this conversation

That makes NO sense! The #ifdef will ALWAYS be false.

2 Likes

When the IDE is launched, the setup() function comes first and then the loop() function -- why have you broken the tradition/style and have put loop() at the top of setup() (post #6) though nothing wrong?

setup is called first only because it is called first by main, you can define functions in any order, makes no difference on execution order

i posted the code i tested
i undef'd the changes i made for my hardware, making it easier for the OP to run the same code on his hardware

so that the more relevant parts of the code will be seen first

I think the point was that there is no point testing afterwards if it's defined as you just undefined it

it's very common to leave test code for debugging later

it's also very common to use ifdefs for different hardware configurations. the define would be on the command line to make/compiler

Fair indeed

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.