Skip to main content
5 of 5
added 3 characters in body
ocrdu
  • 1.8k
  • 3
  • 11
  • 24

Simple if statements are very fast and won't affect performance much even when used in every looping of the loop.

Using an interrupt to implement something simple like if (!amp) amp = 10; will probably make things slower:

  • something will have to keep track of the counter to generate an interrupt, and this "something" takes MCU time, unless it is a peripheral;
  • interrupts have overhead;
  • on single-thread MCUs, interrupts stop regular code execution until they are done;
  • amp = 10; will still have to be executed, in the regular code, or, if you must, in the interrupt.

You would gain more by getting rid of the function call to reset(), but the compiler probably does that for you.

Also, I wouldn't know how to replace a regular if with an interrupt, unless the counter is kept by a (timer) peripheral.

ocrdu
  • 1.8k
  • 3
  • 11
  • 24