Programming Arduino Getting Started with Sketches


Figure 6-6 Connecting a switch to an Arduino board Figure 6-7



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə48/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   44   45   46   47   48   49   50   51   ...   78
Arduino SimonMonk 2011

Figure 6-6
Connecting a switch to an Arduino board


Figure 6-7
Switch with a pull-up resistor
Internal Pull-up Resistors
Fortunately, the Arduino board has software-configurable pull-up resistors built into the
digital pins. By default, they are turned off. So all you need to do to enable the pull-up
resistor on pin 5 for sketch 6-02 is to add the following line:
This line goes in the 
setup
function right after you define the pin as an input. It may
seem a little strange to do a 
digitalWrite
to an input, but this is just the way it works.
Sketch 6-03 is the modified version. Upload it to your Arduino board and test it by
acting like an antenna again. You should find that this time the input stays at 1 in the Serial
Monitor.


Debouncing
When you press a pushbutton, you would expect that you would just get a single change
from 1 (with a pull-up resistor) to 0 as the button is depressed. 
Figure 6-8
shows what can
happen when you press a button. The metal contacts in the button bounce. So a single
button press becomes a series of presses that eventually stabilize.
All this happens very quickly; the total time span of the button press on the oscilloscope
trace is only 200 milliseconds. This is a very “ropey” old switch. A new tactile, click-type
button may not even bounce at all.
Sometimes bouncing does not matter at all. For instance, sketch 6-04 will light the LED
while the button is pressed. In reality, you would not use an Arduino to do this; we are
firmly in the realms of theory rather than practice here.
Figure 6-8
Oscilloscope trace of a button press


Looking at the 

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   44   45   46   47   48   49   50   51   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin