15

I use my Arduino IDE to either upload my sketch to a Arduino or ATTiny or ATmega328. As you know each device can have a different pinout. Does the Arduino compiler support ifdef, depending on the board I am connected to?

For example

#ifdef Attiny85
       a=0; b=1; c=2;
#else
       // arduino
       a=9; b=10; c=11;
#endif
0

1 Answer 1

15

Yes. Here is the syntax:

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
    //Code here
#endif

You can also do something like this for the Mega:

#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
    //Code here
#endif

Assuming the implementation for the ATtiny is correct, your code should be like this:

#if defined (__AVR_ATtiny85__)
       a=0; b=1; c=2;
#else
       //Arduino
       a=9; b=10; c=11
#endif
2
  • In Arduino.h, __AVR_ATtiny85__ is used (capital T). Not sure if it makes any difference though. Commented Sep 13, 2014 at 12:52
  • 2
    alternatively using 1.5.+ IDE you can test against the board type you are building. See Arduino-IDE-1.5---3rd-party-Hardware-specification. Example "#if defined(ARDUINO_AVR_UNO)" or ARDUINO_AVR_MEGA2560 or ARDUINO_AVR_LEONARDO, etc...
    – mpflaga
    Commented Sep 14, 2014 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.