I have an Arduino Uno that I use to irrigate my garden. I have a spare Arduino Micro that I use to develop and test my code before uploading to the Uno. Currently, I have to have two separate projects, one for developing on the Micro and one for uploading to the Uno. (The code I’m developing is compatible across the two platforms and is not very hardware-specific.) This has quickly become quite cumbersome and inefficient to switch between two projects in order to upload to two different platforms.
While remaining in the same project, can I simply change/specify the target board that I’m uploading to?
I have attempted to find an answer to this question. Perhaps it has been answered and I’m missing a few keywords here.
You can just add both boards / environments in your platformio.ini. This is a built-in with PlatformIO. You then either set one or multiple default environments to produce / upload the firmware for one or multiple boards. See Redirecting....
To compile for all boards at the same time, set env_default to promicro5v, promicro3v3, uno. Thus you will see that the firmware compiles on all platforms.
In the code itself, use #ifdef <board macro> to enable board-specific code for each target, e.g. #ifdef ARDUINO_AVR_PROMICRO16 et cetera.
If you want to upload to one specific board, either use the command line to select a specific environment, e.g. pio run -t upload -e uno or set the env_default to the one board environment you wish to upload for.
Wow, thank you! That is precisely what I’m looking for. Also, I never considered using macros for hardware-specific code; that definitely opens up some possibilities.
When you are checking to see if ARDUINO_AVR_PROMICRO16 is defined in your example, is ARDUINO_AVR_PROMICRO16 predefined by the env_default or are you including a #define ARDUINO_AVR_PROMICRO16 at compile time? (Having predefined values would be excellent because then it would be possible to upload for all boards simultaneously while the preprocessor divvies up the hardware specific code according to each compile environment.)
If not predefined, would you define ARDUINO_AVR_PROMICRO16 using build flags? (Allowing you to set up environment-specific defines) If so, could you provide an example of a build flag for ARDUINO_AVR_PROMICRO16?