2

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!");
  }
}
dda
  • 1,595
  • 1
  • 12
  • 17
bets
  • 141
  • 4

2 Answers2

1

To answer the question asked in the title:

#if some_condition
# error Some error message
#endif

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

Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81
0

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.

Michel Keijzers
  • 13,014
  • 7
  • 41
  • 58