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....
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.
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.
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
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,