Hardware Reference
In-Depth Information
value of an input. In this case, it is reading the value of the BUTTON pin. If the
button is being pressed, digitalRead() returns a value of HIGH , or 1 . If it is not
being pressed, it returns LOW , or 0 . When placed in the if() statement, you're
checking the state of the pin and evaluating if it matches the condition you've
declared. In this if() statement, you're checking to see if the value returned by
digitalRead() is LOW . The == is a comparison operator that tests whether the
first item ( digitalRead() ) is equal to the second ( LOW ). If this is true (that is, the
button is not being pressed), the code inside the brackets executes, and the LED
set to LOW . If this is not true (the button is being pressed), the else statement is
executed, and the LED is turned HIGH .
That's it! Program your circuit with this code and confirm that it works as
expected.
Workingwith“Bouncy”Buttons
When was the last time you had to hold a button down to keep a light on?
Probably never. It makes more sense to be able to click the button once to turn it
on and to click the button again to turn it off. This way, you do not have to hold
the button down to keep the light on. Unfortunately, this is not quite as easy as
you might first guess. You cannot just look for the value of the switch to change
from low to high; you need to worry about a phenomenon called switch bouncing .
Buttons are mechanical devices that operate as a spring-damper system. In
other words, when you push a button down, the signal you read does not just
go from low to high, it bounces up and down between those two states for a
few milliseconds before it settles. Figure 2-6 illustrates the expected behavior
next to the actual behavior you might see when probing the button using an
oscilloscope (though this figure was generated using a MATLAB script):
Figure 2-6: Bouncing button effects.
The button is physically pressed at the 25ms mark. You would expect the
button state to be immediately read as a high logic level as the graph on the left
Search WWH ::




Custom Search