Hardware Reference
In-Depth Information
main differences between polling inputs and using interrupts so that you can
decide for yourself which option is best for your particular project.
Ease of Implementation (Software)
Thanks to the excellent programming language that has been constructed for
the Arduino, attaching external interrupts in software is actually very straight-
forward. Using polling to detect inputs to the Arduino is still easier because
all you have to do is call digitalRead() . If you don't need to use hardware
interrupts, don't bother to use them over polling, because it does require you
to write a little more code.
Ease of Implementation (Hardware)
For most digital inputs, the hardware for an input that triggers via polling or
interrupting is exactly the same, because you are just looking for a state change
in the input. However, in one situation you need to adjust your hardware if you
are using an edge-triggered interrupt: bouncy inputs. As discussed in Chapter 2,
“Digital Inputs, Outputs, and Pulse-Width Modulation,” many buttons (some-
thing you will commonly want to use to trigger an input) bounce when you
press them. This can be a significant problem because it will cause the interrupt
routine to trigger multiple times when you want it to trigger only once. What's
worse, it is not possible to use the software debouncing function that you had
previously written because you cannot use a delay() in an interrupt routine.
Therefore, if you need to use a bouncy input with a hardware interrupt, you
need to first debounce it with hardware. If your input does not bounce (like a
rotary encoder) you don't have to worry, and your hardware will be no different
than it was with a polling setup.
Multitasking
One of the primary reasons for using interrupts is to enable pseudo-multitasking.
You can never achieve true multitasking on an Arduino because there is only
one microcontroller unit (MCU), and because it can execute only one command
at a time. However, because it executes commands so quickly, you can use inter-
rupts to “weave” tasks together so that they appear to execute simultaneously.
For instance, using interrupts, you can be dimming LEDs with delay() while
appearing to simultaneously respond to a button input that adjusts the fade speed
or color. When polling an external input, you can only read the input once you
get to a digitalRead() in your program loop, meaning that having “slower”
functions in your program could make it hard to effectively listen for an input.
Search WWH ::




Custom Search