I would like to create a compile error so sketch does not upload in certain cases.

I have written a library with a function that adds pins being used to an array. If the same pin is used twice I want to fail the compilation (preferably with an error that explains why). Is this possible?

void CheckPinNum(int newPinNum){
  for (int i=0; i <= PinArrayLength; i++){
    if (newPinNum == PinArray[i])
      failCompilation("You entered same pin twice!");
  • 1
    Do you know the difference between compile time and execution time?
    – user31481
    Commented Jan 22, 2018 at 9:43
  • 4
    I'm voting to close this question as off-topic because it contains fundamental errors about compiling and executing programs.
    – user31481
    Commented Jan 22, 2018 at 9:45
  • @LookAlterno If you vote that way, please be kind enough to explain to me how I should ask the question to get where I want.
    – bets
    Commented Jan 22, 2018 at 9:47
  • 1
    How could this be done? What fundamental mechanisms are available in C++? Actually there is. It is called static_assert en.cppreference.com/w/cpp/language/static_assert. To use it in this case the compiler must be able to evaluate the function at compile-time. This requires the pin number vector to be const but then adding a pin makes no sense (as a compile-time error:). An example of using static_assert can be found here github.com/mikaelpatel/Cosa/blob/master/cores/cosa/Cosa/… Commented Jan 22, 2018 at 10:27
  • 1
    @bets. The question has been answered but I want to point out that C++ is not the hardest language to learn. A major factor in assessing the difficulty of learning a language is the order in which you learn them. For example, If you want to go the C/C++ route then learning assembly first makes learning C easy, especially when dealing with pointers which is the hardest thing for many novices. Secondly, Arduino is a great way to get into programming (and electronics) Just about anything you can think of has been done with them and there is a huge community out there willing to help.
    – atland
    Commented Jan 26, 2018 at 6:46

2 Answers 2


It's impossible to check dynamic behavior compile time.

However, the best 'alternative' I can think of, is during the setup set all pin numbers (if that is possible).

And if something is wrong, let the 'user' or client know something is wrong. In extreme cases, just by putting the Arduino in an endless loop, in best cases, send a serial line, blink a light or whatever feedback.

You can do the same later on (when CheckPinNumber needs to be called in the loop, so not at setup), but the sooner you can do these checks, the better.


To answer the question asked in the title:

#if some_condition
# error Some error message

But note that this technique only works at compile time, which probably makes it useless for you.

  • Thanks! "compile time" is good for me. I'm not sure I totally understand this answer. But I will try to figure out how to check the vars entered in the sketch this way.
    – bets
    Commented Jan 22, 2018 at 9:55
  • 2
    Pay attention that the some_condition must be a constant integer expression (not using any variable or function call) computable at compile time.
    – user31481
    Commented Jan 22, 2018 at 10:01

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