How to create Constants that can be used across an Arduino project
Create a new header and CPP file and define the constants. To use the constants, include the header file in your script and implement the constant.
The code for the following tutorial is found on Github through our Open Source Arduino starter project. This can be cloned using the command below:
git clone git@github.com:delasign/arduino-starter-project.git
Step One: Create the Files
Create the Header and CPP file for the Constants.
Step Two: Declare the Constants
Add the constants to the header file.
The only thing that you need to add to the CPP file is the #include constants.h line.
Step Three: Implement the Constants
Wherever you wish to implement the constant:
- Include the Constants.h script.
- Implement the constant (i.e.SAMPLE_CONSTANT) in a function.
Step Four: Test
Upload the code onto your board and confirm that the constant works as expected.
Looking to learn more about things you can do with Arduino ?
Search our blog to find educational content on Arduino.