Define options

Updated on 23 September 2022
dev board Arduino UNO
firmware Arduino
chip ATmega328P
features define variables options arduino-cli flags
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Buy the components

Code

Download code define-options.ino
byte localAddress = LOCAL_ADDRESS;
byte destinationAddress = DESTINATION_ADDRESS;

void setup() {
  Serial.begin(9600);
  Serial.println("Define options...");
}

void loop() {
  Serial.println("\nLocal address: " + String(localAddress, HEX));
  Serial.println("Destination address: " + String(destinationAddress, HEX));
  delay(1000);
}

Makefile

BOARD?=arduino:avr:uno
PORT?=/dev/cu.usbmodem14*
BUILD=build

.PHONY: default lint all flash clean

default: lint compile_a flash clean

a: lint compile_a flash clean

b: lint compile_b flash clean

compile_a:
	arduino-cli compile --clean --fqbn $(BOARD) --build-property compiler.cpp.extra_flags="-DLOCAL_ADDRESS=0xAA -DDESTINATION_ADDRESS=0xBB" --output-dir $(BUILD) ./

compile_b:
	arduino-cli compile --clean --fqbn $(BOARD) --build-property compiler.cpp.extra_flags="-DLOCAL_ADDRESS=0xBB -DDESTINATION_ADDRESS=0xAA" --output-dir $(BUILD) ./

lint:
	cpplint --extensions=ino --filter=-legal/copyright,-whitespace/line_length,-readability/casting,-readability/todo,-runtime/int *.ino

flash:
	arduino-cli upload -p $(PORT) --fqbn $(BOARD) --input-dir $(BUILD) --verbose

clean:
	rm -r build

Prototype

A photo of the actual setup.

Define options prototype

Serial console

Serial output from the firmware.

Define options serial console

Description

Create options in using different variable values with #define statement. The advantage of this method is that the same codebase can be used for different options.

How to run the example

Run command make a to put the LOCAL_ADDRESS value as 0xAA.

Run make b to put the LOCAL_ADDRESS value as 0xBB.

The make command will run the arduino-cli compile with options to #define the LOCAL_ADDRESS and DESTINATION_ADDRESS values.

arduino-cli compile --clean --fqbn $(BOARD) --build-property compiler.cpp.extra_flags="-DLOCAL_ADDRESS=0xBB -DDESTINATION_ADDRESS=0xAA" --output-dir $(BUILD) ./

References