Using Project Properties as defines in your code

You can define constants in your project properties. You can then use these constants in your sketch to control your sketch's behavior depending on these constants.

Simple defines

In your Project Properties window, you can specify defines that behave like #define directives in your source code.

Instead of writing

#define USBPORT 4

you can enter the define in your project properties:

Project Properties defines

Values in the "Defines - Configuration" field are configuration dependent, you can have different defines in your sketch, depending on your configuration (e.g. "Debug" and "Release").

You can also enter values in the field below, named "Defines - Project", these are configuration independent.

You can then use these defines like ordinary #defines written into the code, like in this example:

#if USBPORT > 3
....your code here....
#endif

Using Multiple Defines

Multiple defines can be used by separating each entry added to the Project Properties with a semi colon:

USBPORT 4;BLINKMS 1000

Using build properties as defines

Build properties are parameters that control the compilation and upload process. You can take advantage of these build properties and use them in project property defines. Refer to the build properties by putting them in curly braces:

Project Properties defines

Visual Micro will replace {upload.maximum_size} with the actual value of that build property, e.g. 30720 in case of an Arduino Nano board.

An equivalent hardcoded #define in the code would look like this:

#define PROGMEM_SIZE 30720 

With this type of defines, you can, for example, adjust your sketch to the board you are using without changing your source code. With the PROGMEM_SIZE example above, you could compile parts of your sketch only if the board's program memory is big enough:

#if PROGMEM_SIZE > 50000
....your code here....
#endif

Where do I find the build properties?

You can create a complete list of the available build properties with this option:

         Tools > Options > Visual Micro > Compiler > Show Build Properties

With your next build (using [F7]), you will get a complete list of the build properties in the Output Window.

Converting defines into strings

The following tip applies to C/C++ preprocessor defines in General and is supported by modern compilers.

If you want to use a define as a string, you can use the compiler's "Stringification" function, example:

#define TOSTRING( X ) #X
char progmemSizeString[] = "Progmem size is " TOSTRING(PROGMEM_SIZE);

The TOSTRING macro puts quotes around the value, so that 30720 becomes "30720". This lets you use build properties as C/C++ strings.

The above lines of code are equivalent to:

char progmemSizeString[] = "Progmem size is 30720";

Note that "Stringification" only works in preprocessor macros, that's why we need the TOSTRING macro here.
You cannot write "Progmem size is " #PROGMEM_SIZE