I don't understand this part of code

Hi,.

I'm a newbie in the programming of Arduino. I'm studing the library DHT.h library by pillar1989 for the AM2302 humidity and temperature sensor.

Thre is a peace of code in the declaration zone that say:

#if defined(ARDUINO_ARCH_AVR)
#define debug Serial

#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM)
#define debug SerialUSB
#else
#define debug Serial
#endif

I can not understand this peace of code, Could anybody clear me?

Thanks in advance!

wi1996:
Hi,.

I'm a newbie in the programming of Arduino. I'm studing the library DHT.h library by pillar1989 for the AM2302 humidity and temperature sensor.

Thre is a peace of code in the declaration zone that say:

#if defined(ARDUINO_ARCH_AVR)
#define debug Serial

#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM)
#define debug SerialUSB
#else
#define debug Serial
#endif

I can not understand this peace of code, Could anybody clear me?

Thanks in advance!

assuming you know how to use '#define' the other stuff in that snippet are preprocessor conditionals. you can do a search for them if you want to learn more....

hope that helps

Those lines are defining the object used for the library's debug output.

ARDUINO_ARCH_AVR, ARDUINO_ARCH_SAMD, and ARDUINO_ARCH_SAM are macros that identify the architecture of the board that is being compiled for. For example, the Arduino Uno uses the AVR architecture, so when you compile with Tools > Board > Arduino Uno selected, ARDUINO_ARCH_AVR will be defined. The Arduino Due uses the SAM architecture, so when you compile with Tools > Board > Arduino Due selected, ARDUINO_ARCH_SAM will be defined. The Arduino MKR Zero uses the SAMD architecture, so when you compile with Tools > Board > Arduino Zero (Native USB Port) selected, ARDUINO_ARCH_SAMD will be defined.

If you are compiling for the Uno, this line will take effect:

#define debug  Serial

If you are compiling for the Due or Zero, this line will take effect:

#define debug  SerialUSB

#define causes a text replacement in your sketch during preprocessing. So if you have this:

#define debug  Serial

then anywhere in your sketch the name debug occurs, it will be replaced by Serial. For example, this code:

debug.println("Some debug output");

becomes:

Serial.println("Some debug output");

On the Uno, the serial port used for communication with the computer is named Serial. On the Due and Zero, when you have it connected to your computer via the Native USB Port, the name is SerialUSB. So the library author wanted some code that would automatically adjust their debug code to work with the different boards they were using for testing.

1 Like

Dear Pert:

Thanks so much for your explanation. Therefore, If I plug any ardunino card and in the IDE and I select this card, this part of the code can be removed?.

I want to mean, when you select in the IDE the arduino card you are using for your project, you are warning the compiler the used architecture, and therefore, no need this code. It can be erased.

Can you please confirm?

Thanks in davance!

wi1996:
you are warning the compiler the used architecture, and therefore, no need this code. It can be erased.

Which code are you referring to when you say "this code"?

wi1996:
I'm studing the library DHT.h library by pillar1989 for the AM2302 humidity and temperature sensor.

Please post the link to the library. Or if you installed it via Library Manager, give the full name of the library.

pert:
Which code are you referring to when you say "this code"?
Please post the link to the library. Or if you installed it via Library Manager, give the full name of the library.

I refer to this

#if defined(ARDUINO_ARCH_AVR)
#define debug Serial

#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM)
#define debug SerialUSB
#else
#define debug Serial
#endif

Here is the library I want to refer:

Please check line 25 to 32.

Thanks so much for your help!

@wi1996, what problem are you trying to solve?

wi1996:
I refer to this

Those are all preprocessor directives. It is handled by the preprocessor. The compiler doesn't see any of that code.

wi1996:
Grove_Temperature_And_Humidity_Sensor/examples/DHTtester/DHTtester.ino at master · Seeed-Studio/Grove_Temperature_And_Humidity_Sensor · GitHub

Hah! That sketch doesn't even compile. During what was claimed to only be code formatting:

they changed the macro name in the #define directives from SERIAL to debug, but didn't update the rest of the code. To fix their screw-up, change:

#if defined(ARDUINO_ARCH_AVR)
    #define debug  Serial

#elif defined(ARDUINO_ARCH_SAMD) ||  defined(ARDUINO_ARCH_SAM)
    #define debug  SerialUSB
#else
    #define debug  Serial
#endif

to:

#if defined(ARDUINO_ARCH_AVR)
    #define SERIAL  Serial

#elif defined(ARDUINO_ARCH_SAMD) ||  defined(ARDUINO_ARCH_SAM)
    #define SERIAL  SerialUSB
#else
    #define SERIAL  Serial
#endif

thanks guys. I'm trying to learn arduino code and I dropped in that web side (don't know exactly why). I started to check that code for curiosity and I couldn't understand that piece,

Now it is clear. Thanks!