13

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
PhillyNJ
  • 1,128
  • 3
  • 10
  • 20

1 Answers1

13

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
Anonymous Penguin
  • 6,155
  • 9
  • 31
  • 62
  • In `Arduino.h`, `__AVR_ATtiny85__` is used (capital `T`). Not sure if it makes any difference though. – geometrikal Sep 13 '14 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](https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5---3rd-party-Hardware-specification#boardstxt). Example "#if defined(ARDUINO_AVR_UNO)" or ARDUINO_AVR_MEGA2560 or ARDUINO_AVR_LEONARDO, etc... – mpflaga Sep 14 '14 at 03:55