EasyManuals Logo

Arduino uno User Manual

Arduino uno
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #71 background imageLoading...
Page #71 background image
After initializing the button and LED pins, we declare two variables:
old_but-
ton_state
stores the previous state of our pushbutton, and
led_state
stores the
LEDs current state. Both can be either
HIGH
or
LOW
.
In the
loop
function, we still have to read the current button state, but now
we not only check whether it is
HIGH
, but we also check whether it has changed
since the last time we read it. Only when both conditions are met do we toggle
the LEDs state. So, we no longer turn the LED on and off over and over again
as long as the button is pressed. At the end of our program, we have to store
the buttons current state in
old_button_state
.
Upload the new version, and youll see that this solution works much better
than our old one. But you will still find some cases when the button doesnt
behave fully as expected. Problems mainly occur in the moment you release
the button.
These problems occur because the mechanical buttons bounce for a few
milliseconds when you press them. In the following figure, you can see a
typical signal produced by a mechanical button. Right after you have pressed
the button, it doesnt emit a clear signal. To overcome this effect, you have
to debounce the button. Its usually sufficient to wait a short period of time
until the buttons signal stabilizes. Debouncing ensures that the input pin
reacts only once to a push of the button:
Button pressed
Button released
5 V
0 V
In addition to debouncing, we still have to store the current state of the LED
in a variable. Heres how to do that:
BinaryDice/DebounceButton/DebounceButton.ino
const unsigned int BUTTON_PIN = 7;
Line 1
const unsigned int LED_PIN = 13;
-
void setup() {
-
pinMode(LED_PIN, OUTPUT);
-
pinMode(BUTTON_PIN, INPUT);
5
}
-
-
int old_button_state = LOW;
-
int led_state = LOW;
-
10
void loop() {
-
Chapter 3. Building Binary Dice 52
report erratum discuss
www.it-ebooks.info

Table of Contents

Other manuals for Arduino uno

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Arduino uno and is the answer not in the manual?

Arduino uno Specifications

General IconGeneral
Form factorArduino
CertificationRoHS, FC, CE
Processor model-
Processor frequency- MHz
Microcontroller modelATmega328
Microcontroller frequency16 MHz
DC input voltage7-12 V
Operating voltage5 V
DC current per I/O pin40 mA
Flash memory0.032 MB
Maximum internal memory- GB
SRAM (Static Random Access Memory)2 KB
EEPROM (Electrically Erasable Programmable Read-Only Memory)1 KB
Wi-FiNo
Number of analog I/O pins6
Number of digital I/O pins14
Weight and Dimensions IconWeight and Dimensions
Board dimensions53.4 x 68.6 mm

Related product manuals