#ifdef for a sketch to run on different arduino models

I'm sure there is an easy guide somewhere but cant get the search terms right. I did find this but I dont find it much help

I'm trying to write a sketch that will run without modification on basic arduino versions - eg the uno, nano, micro, perhaps the ESP32 & ESP8266 boards.

I THINK (well guess) I'll need a section a bit like this:

#if defined(ARDUINO_AVR_UNO)
  // Uno pin assignments
#elif defined(ARDUINO_AVR_PRO)
  // Pro Mini assignments

I'll need to define just a few pins - maybe on-board LED, a couple of input pins, and an analog input.
does anyone have an example I could use as a guide rather than me start from scratch?

There are many libraries who do this and you could take as an example. E.g. my MobaTools lib. Look into MobaTools.h to distinguish between different architectures.
Usually there are defines for the architecture and for the specific board. One way to see what is defined is to mark 'Show verbose output during compilation'. These defines are set as -D options when starting gcc, which you can see when this mark is set.
E.g. for a Nano:

Thanks #MicroBahner but I'm not much further forward.
#elif defined ARDUINO_ARCH_ESP32 - I know this refers to the ESP32 boards, but there are different ones with different pin-outs.
I DONT know how to write a similar define for eg a Micro Pro board.
And I've looked at libraries 'til I'm getting snow blind, without finding something simple that sets up an IO library to cover a range of "beginner" boards like the Uno, MEga, Nano & Micro.
@jremington I remember you showed me a library (or sketch) like this a while back?

Did you have a look at the 'Show verbose output during compilation'? Every information you search is contained there. E.g. for the ESP32 Pico board:
And there are even more board specific defines.
The lines with this information are fairly long. The easiest way ist to copy one line of a compiler call into an editor and search for '-D'
Of course you must have the boards installed and selected which you are searching for.
It's not as easy to find this information in the board specific core files, because these defines are concatenated from different sources. So in my opinion the easiest way is to have a look at the compiler calls.

see List of Arduino board preprocessor #defines

Its well buried but I guess the -D signifies a #define so eg below I have

Part of compilation message:

Compiling sketch...
"C:\ArduinoIDEPortable\portable\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_MICRO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8037 "-DUSB_MANUFACTURER="Unknown"" "-DUSB_PRODUCT="Arduino Micro"" "-IC:\ArduinoIDEPortable\portable\packages\arduino\hardware\avr\1.8.3\cores\arduino" "-IC:\ArduinoIDEPortable\portable\packages\arduino\hardware\avr\1.8.3\variants\micro" "C:\Users\johnl\AppData\Local\Temp\arduino_build_622011\sketch\readanalogvolts.ino.cpp" -o "C:\Users\johnl\AppData\Local\Temp\arduino_build_622011\sketch\readanalogvolts.ino.cpp.o"

Thanks, @gcjr thats useful.

Yes of course. That's what I tried to tell you here in #2:

@MicroBahner @gcjr Thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.