How to detect compilation for an ATTINY85 vs NANO?

I have a sketch I'm writing for an ATTiny85 but I like to test the code on a nano to keep from having to move the ATTiny chip from the programmer back to it PCB and when the code is right based on the test from the nano, I change the pinout assignments for the external hardware and then upload to the ATTiny then put it in the PCB...

I'd like to be able to add to the sketch, some way of detecting which chip I'm compiling for (the Nano or the ATTiny85) and then set the pin assignments accordingly.

How can I do this?

Thank you,

Mike

Look into conditional compilation for Arduino. Here is a page, among others, that I found with a Google search >> Arduino Preprocessor Directives Tutorial - ifdef & endif

Look for section 5. Conditional compilation: #if directives.

I'm familiar with how to do that in C++, what I don't know, is how to identify an ATTINY85 or a NANO in the #if expression. Where would I find that reference? I did look for it but to no avail.

Use the __AVR_ATtiny85__ and __AVR_ATmega328P__. They have two underscores at the begin and end.

There are two options:

#if defined (__AVR_ATtiny85__)
  Debug.println("__AVR_ATtiny85__ defined");
#endif

#ifdef __AVR_ATtiny85__
  Debug.println("__AVR_ATtiny85__ defined");
#endif

and a #else is also possible.
They are part of the "preprocessor directives" of the C and C++ language.

1 Like

OK, Thank You

:slight_smile:

For safety, you could add this:

#if !defined (__AVR_ATtiny85__) && !defined (__AVR_ATmega328P__)
  #error Wrong chip, it must be a ATtiny85 or ATmega328P
#endif
1 Like

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