I could use some help with the syntax of a conditional compile.
#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_HTU21D
#if SENSOR_TYPE == SENSOR_BMP180
#include <BMP180.h>
BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D
#include <HTU21D.h>
HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
#endif
If the sensor_type is BMP180 (the first one) it compiles. If the sensor_type is HTU21D or DS18B20 I get a ton of compile errors like 'suchAndSuch' was not declared in this scope as if never finds the #endif or like a close brace is missing.
According to some google searches I've done, this should work as I have posted it here, but it doesn't. I've tried a variety of formats including using () and can't seem to find the correct one.
Problem is NOT with HTUD21D.h, as my HTUD21D_testbed and DS18B20_testbed projects both compile and run just fine. Further testing seems to confirm it is a problem with the conditional compile. Note the following:
1) I finished the DS18B20 section. The following will not compile:
#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_DS18B20
#if SENSOR_TYPE == SENSOR_BMP180
#include <BMP180.h>
BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D
#include <HTU21D.h>
HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
#define ONE_WIRE_BUS 15 // Data Pin that DS18B20 is connected to
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature weather(&oneWire);
#endif
But this will:
#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_DS18B20
#define ONE_WIRE_BUS 15 // Data Pin that DS18B20 is connected to
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature weather(&oneWire);
Likewise, if I move the DS18B20 block to the top, it will compile
#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_DS18B20
#if SENSOR_TYPE == SENSOR_DS18B20
#define ONE_WIRE_BUS 15 // Data Pin that DS18B20 is connected to
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature weather(&oneWire);
#endif
#if SENSOR_TYPE == SENSOR_BMP180
#include <BMP180.h>
BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D
#include <HTU21D.h>
HTU21D weather;
#endif
Same tricks apply to the HTU21D block. Commenting out the BMP180 block above it and the DS18B20 block below it gets it to compile. Moving the DS18B20 block to the top gets it to compile. But with it in the middle (or at the bottom) it will not compile.
Won't Compile:
#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_HTU21D
#if SENSOR_TYPE == SENSOR_BMP180
#include <BMP180.h>
BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_HTU21D
#include <HTU21D.h>
HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
#define ONE_WIRE_BUS 15 // Data Pin that DS18B20 is connected to
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature weather(&oneWire);
#endif
Will Compile:
#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_HTU21D
#include <HTU21D.h>
HTU21D weather;
and also will compile: (moved to top)
#define SENSOR_NONE 0
#define SENSOR_BMP180 1 // Set to 1 BMP180 Barometric Pressure sensor is installed -- OR
#define SENSOR_HTU21D 2 // set to 2 if HTU21D Temp & Humidity sensor is installed -- OR
#define SENSOR_DS18B20 3 // Set to 3 if DS18B20 Temp sensor is installed on the TinyRTC
#define SENSOR_TYPE SENSOR_HTU21D
#if SENSOR_TYPE == SENSOR_HTU21D
#include <HTU21D.h>
HTU21D weather;
#endif
#if SENSOR_TYPE == SENSOR_BMP180
#include <BMP180.h>
BMP180 weather;
#endif
#if SENSOR_TYPE == SENSOR_DS18B20
#define ONE_WIRE_BUS 15 // Data Pin that DS18B20 is connected to
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature weather(&oneWire);
#endif
So in summary, if the topmost of the 3 blocks is the active one with the true condition, it will compile. If the second or 3rd block has the true condition, it will not compile. If I comment out the inactive blocks it will compile. (Oh, and yes, I've tried some white space between them).
What I have tried:
Parentheses: #if (SENSOR_TYPE == SENSOR_HTU21D)
The real value: #if SENSOR_TYPE == 2
Single equal: #if SENSOR_TYPE = SENSOR_HTU21D