Hardware Reference
In-Depth Information
Hardware interrupts work by “attaching” interrupt pins to certain functions.
So, the second argument of attachInterrupt() is a function name. If you want
to toggle the state of a Boolean variable every time an interrupt is triggered,
you might write a function like this, which you pass to attachInterrupt() :
void toggleLed()
{
var = !var;
}
When this function is called, the Boolean var is toggled to the opposite of its
previous state, and the rest of your program continues running where it left off.
The final argument passed to attachInterrupt() is the trigger mode. Arduino
interrupts can be triggered on LOW , CHANGE , RISING , or FALLING . (The Due can
also be triggered on HIGH .) CHANGE , RISING , and FALLING are the most common
things to trigger on because they cause an interrupt to execute exactly one time
when an external input changes state, like a button going from LOW to HIGH .
The transition from LOW to HIGH is RISING , and from HIGH to LOW is FALLING . It
is less common to trigger on LOW or HIGH because these cause the interrupt to
fire continuously as long as that state is true, effectively blocking the rest of the
program from running.
BuildingandTestingaHardware-DebouncedButton
InterruptCircuit
To test out your newfound knowledge, you construct a circuit with an RGB
LED and a hardware-debounced pushbutton. The LED fades up and down on
a selected color. When the button is pressed, the LED immediately changes the
fade color to another one, while using delay() to accomplish the fading.
Creating a Hardware-Debouncing Circuit
As you learned in the Chapter 2, most buttons actually “bounce” up and down
when you press them. This action presents a serious problem when you are
using hardware interrupts because it might cause an action to be triggered more
times than you intended. Luckily, you can debounce a button in hardware so
that you always get a clean signal going into your microcontroller.
First, take a look at an ordinary button signal hooked up using a pull-up
resistor. Using a pull-up resistor instead of a pull-down does exactly what you
would expect: By default, the button state is pulled high by the resistor; when
the button is pressed, it connects ground to the I/O pin and input goes low.
Search WWH ::




Custom Search