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,553
  • 1
  • 12
  • 17
bets
  • 141
  • 4
  • 1
    Do you know the difference between compile time and execution time? –  Jan 22 '18 at 09:43
  • 4
    I'm voting to close this question as off-topic because it contains fundamental errors about compiling and executing programs. –  Jan 22 '18 at 09: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 Jan 22 '18 at 09:47
  • It's not **how** you asked it, but **what** you asked for. You want a compiler that can pre-evaluate something that will be unknow until execution time. That's is impossible in general (see Turing's Halting Problem). And, how do you pretend to tell the compiler about your particular restrictions imposed over the data? Where do you intend to put that info? –  Jan 22 '18 at 09:55
  • @LookAlterno So in that case you can write an answer like "This is not possible because a compiler can't pre-evaluate the variables written in the sketch". Or suggest someway that can work. Thanks anyway. – bets Jan 22 '18 at 10:01
  • Or you pass your Programming 101 course. This about Arduino, not a programming school. –  Jan 22 '18 at 10:06
  • @LookAlterno Are you not aware that many people using Arduino may have never taken any course and they learn as they go? Anyway, the "Be Kind 101" course is a lot more fundamental. – bets Jan 22 '18 at 10:17
  • 1
    How could this be done? What fundamental mechanisms are available in C++? Actually there is. It is called static_assert http://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 https://github.com/mikaelpatel/Cosa/blob/master/cores/cosa/Cosa/Queue.hh#L36 – Mikael Patel Jan 22 '18 at 10:27
  • @bets. Learning to program with Arduino is bad idea: you chose the hardest programming language posible (C++), with subpar IDEs, skinny documentation, and no debugging capabilities at all. I'm sorry not being kind enough for you. –  Jan 22 '18 at 11:11
  • 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 Jan 26 '18 at 06:46

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
  • 39,449
  • 4
  • 36
  • 72
  • 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 Jan 22 '18 at 09: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. –  Jan 22 '18 at 10:01
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
  • 12,759
  • 7
  • 37
  • 56