How to compile/upload the same code for different hardware?

I have three prototype boards that I built, but one is an oddball in that the ESP32S3 I used had some non-functioning GPIOs as a result of soldering/unsoldering/soldering, so I had to move those signals to other pins. I want to use the same code base for these boards.

I thought about using a #ifdef, #elif, #else directives with a couple of #defines in the main .ino file. However, I have to comment/uncomment the correct #define for each board before I compile and upload. Seems fraught with potential for error. Is there a better way?

For example, in main.ino, I have to comment out one of these before each compile/upload depending on the board.

#define Board_Type_A
#define Board_Type_B

rotary_encoder.h

#ifdef Board_Type_A
byte pin[] = {8,3,9,5,6,17,16};  //   Seven segment pin is connected to GPIO pins: 8,3,48,5,42,38,39 to a,b,c,d,e,f,g

#elif defined(Board_Type_B)
byte pin[] = {8,3,48,5,42,38,39};   //   Seven segment pin is connected to GPIO pins: 8,3,48,5,42,38,39 to a,b,c,d,e,f,g
#endif

Thanks!

Do you have a spare port pin that you could use as an input? If so then maybe you could set its mode to INPUT_PULLUP and connect it to Gnd on one board type, and leave it pulled up on the other board type.

Or pehaps you could do something similar by checking the ESP32’s mac address and deciding which port pins to use based on that.

Great ideas!

I have to think through the boot up sequence issues. I define pin numbers in .h files, and then enable the pins in setup(). The spare GPIO you mention would not become active until setup(), so I need to think through the boot sequence to see if that is an issue.

Have you done this before?

I think you could have the first line of setup() set the pinMode of the spare pin to INPUT_PULLUP. Then you could have a global bool value set to true or false in the next line of setup() depending on whether a read of the spare pin returned HIGH or LOW.

After doing that, you could then use the value of that bool to decide whether to use the pin allocations for a type A or type B board.

No

If it's different types of boards, they usually have some specific macros; some more intentional than others: SOMETHING_ESP32 versus ANOTHER_AVR that can be checked with #ifdef. Hard part is figuring out those names and exactly how they apply.

For individual boards as in your specific case: on a Mac, when you plug in a device, the result added to /dev has the USB serial number: e.g. /dev/cu.usbmodemABCDEF1234. (cu means "calling unit").

The compile command could be augmented to have an additional macro defined, effectively

-DSERIAL_NUMBER_$(some_shell_script_that_finds_unique_dev_cu)

so that in your code

#ifdef SERIAL_NUMBER_ABCDEF1234

No such luck on Linux (often always added as '/dev/ACM0') or Windows (no /dev, COM port is persistent) though. In place of a simple script, there's probably a utility that can return the serial number of every connected USB device; filter through those....

Since they're all ESP32s, I'd start by looking at MacAddress, which ought to be unique for each one, I'd think.

I think the time you have spent trying to figure out a solution, adding to that the time of other forum members, has already exceeded the replacement cost of the damaged ESP32!

My apologies for disturbing your nap time. But then I have to build a new prototype test board. More parts more time.

My project is spread over 10 file: 5 .ino and 5 .h. How can I find the order that the files are mashed together to make sure I put the board type pin in the right file so it is tested before the file with the pin assignments?

My apologies for drawing attention to your innumeracy :wink:

Make another .h file for your board type pin. Include that at the top of each of the other .h files.

I was suggesting you made the pin choice during setup() at run time, not at compile time. You would of course have to change your code so that the pin numbers became variables rather than #define’s or const’s. Those pin number variables would have their values set at the start of setup(), the rest of the code would use the variables whenever referencing pin numbers.

Dave’s plan plan worked flawlessly. Thank-you!

For some reason my response from earlier did not post.

Dave_Lowther's solution worked flawlessly.

Thanks, Dave!!