Conditional inclusion problem (#ifdef)

Hello,

I usually create define based inclusion(s) in my sketches, but now I'm stuck with this one.

#define EXT_PIR_SENSOR

#ifdef EXT_PIR_SENSOR
const uint8_t PIR_SENSOR_PIN = A12;
const unsigned int PIR_THRESHOLD_RAW = 410u;
#endif

void setup()
{
  analogReference(DEFAULT);
}

void loop()
{

#ifdef EXT_PIR_SENSOR
  managePirSensor();
#endif
}

void managePirSensor()
{
  int pirValue = 0;
  pirValue = analogRead(PIR_SENSOR_PIN);
  if (pirValue >= PIR_THRESHOLD_RAW)
  {
    //do something
  }
}

If EXT_PIR_SENSOR is defined, the sketch compiles. But if I remove it, the compiler misses PIR_SENSOR_PIN and PIR_THRESHOLD_RAW from managePirSensor() function. This function should not be called at all! Even if I comment it in the loop, the error is still there. It drives me crazy. :upside_down_face:

Could someone help me out?

This method used to work in my sketches. It is convenient for me to create "options" in the sketch (for example printing to serial monitor during testing and simply remove the define if printing is not needed).

Thanks in advance.

#ifdef EXT_PIR_SENSOR
  managePirSensor();
#endif
}
#ifdef EXT_PIR_SENSOR
void managePirSensor()
{
  int pirValue = 0;
  pirValue = analogRead(PIR_SENSOR_PIN);
  if (pirValue >= PIR_THRESHOLD_RAW)
  {
    //do something
  }
}
#endif

It's nothing to do with being called, you defined it, and compiler tried to compile it

Tu en es sûr?

Why not condition the function itself?

Even it if should be optimised away, it still would have to be correct and compile… which is why I doubt it ever worked exactly like that.

a7

Oooh my bad. :zipper_mouth_face:

You are all right, I used to condition the function itself also.

Thanks for the quick help!

By the way, if defines already came up. :slight_smile:

Could someone tell me where these A0, A1, A2, etc. defines are?
I searched the C:\Program Files (x86)\Arduino\hardware\arduino\avr\ directory but couldn't find it.

Search wider or better!

I found

pins_arduino.h (6.3 KB)

in

Arduino.app/Contents/Java/portable/packages/arduino/hardware/avr/1.8.2/variants/standard

searching for A0, which turnt up a bunch of stuff, scanned that by eye.

a7

Nice, thank you @Coding_Badly and @alto777.

Now I know where did I miss it...I searched for "#define A12"...

Yeah, it's a tradeoff. I just searched for "A0" , took two refinements to narrow in.

If you had been lucky, it would have turned up in dozens of files (only). As it was even so much as a space between the '#' and the 'd' woulda left you with nothing.

Of course it should just be a matter of selecting it in you sketch and using the "Find Declaration" option in the Search sub menu. :expressionless:

a7