Download Digital Inputs (Switches)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Flip-flop (electronics) wikipedia , lookup

Switched-mode power supply wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Buck converter wikipedia , lookup

Switch wikipedia , lookup

Opto-isolator wikipedia , lookup

Rectiverter wikipedia , lookup

Transcript
Khaled A. Al-Utaibi
[email protected]

The Push Button

Interfacing Push Buttons to Arduino

Programming Digital Inputs

Working with “Bouncy” Buttons




The simplest form of digital input is a push
button.
You can insert these directly into your
breadboard as shown in the next slide.
A push button allows a voltage or current to pass
when the button is pressed.
Push buttons are generally used as reset
switches and pulse generators.

A push button can be interface to a circuit using:
− (1) Pull-Up Resistors: In this case, the output of the button
is normally HIGH and when the button is pressed it
generates a LOW pulse (Figure 1).
−(2) Pull-Down Resistors: In this case, the output of the
button is normally LOW and when the button is pressed it
generates a HIGH pulse (Figure 2).

Programming digital inputs/outputs involves two
operations:
−(1) Configuring the desired pin as either input or
output.
−(2) Controlling the pin to read/write digital data.

Because you’ll generally dedicate each pin to
serve as either an input or an output, it is
common practice to define all your pins as inputs
or outputs in the setup.
// set pin 4 as a digital input
void setup() {
pinMode(9, INPUT);
}

The pinMode() function configures the specified
pin to behave either as an input or an output.
−Syntax:
 pinMode(pin, mode)
−Parameters:
 pin: the number of the pin whose mode you wish to set.
 mode: INPUT or OUTPUT
−Returns:
 None

Because the loop() function runs over and over
again, inputs are usually read in this function.
// read the state of pin 4
int pinState;
void loop() {
pinState = digitalRead(4);
}

The digitalRead() function reads the value from
a specified digital pin, either HIGH or LOW.
−Syntax:
 digitalRead(pin)
−Parameters:
 pin: the number of the digital pin you want to read (int) .
−Return:
 HIGH or LOW

Write an Arduino program that repeatedly reads
the state of a push button connected to pin 4
and display the message “Button Pressed” on the
Arduino environment’s built-in serial monitor
whenever the button is pushed.
// Repeatedly Reads the state of a bush button connected to pin 4,
// and displays the message “Button Pressed” whenever the button
// is pushed.
int buttonState;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an input.
pinMode(4, INPUT);
// initialize and start the serial port
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
buttonState = digitalRead(4);
// read the state of pin 4
if (buttonState == HIGH) {
// if pin state is HIGH
Serial.println(“Button Pressed”); // display the message
}
}

Push buttons exhibit a phenomenon called switch
bounce, or bouncing, which refers to a button’s
tendency to turn on and off several times after being
pressed only once by the user.

This phenomenon occurs because the metal contacts
inside a push button are so small that they can
vibrate after a button has been released, thereby
switching on and off again very quickly.

Switch Bounce can cause wrong behavior of digital
circuits due to reading multiple values for a single
press of the button.
Ideal Switch (No Bounce)
Switch Bounce

There are two approaches for cleaning up switch
bounce:
−(1) Hardware Debounce
−(2) Software Debounce

It is relatively straightforward to deal with switch
bounce problem in software.

Hence, we will cover only software switch
debouncing.

Basic Idea:
−look for a button state change, wait for the bouncing to
finish, and then reads the switch state again.

This software logic can be expressed as follows:
−1. Store a previous button state and a current button state
(initialized to LOW).
−2. Read the current button state.
−3. If the current button state differs from the previous
button state, wait 5ms because the button must have
changed state.
−4. After 5ms, reread the button state and use that as the
current button state.
−5. Set the previous button state to the current button state.
−6. Return to step 2.
// Modify previous program to debounce the switch
int buttonState;
// the current reading from the input pin
int previousButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0;
// the last time the output pin was toggled
long debounceDelay = 50;
// the debounce time;
void setup() {
pinMode(4, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(4);
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// the reading has been there for longer than the debounce delay, so take it
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
// if pin state is HIGH
Serial.println(“Button Pressed”); // display the message
}
}
}
// save the reading
lastButtonState = reading;
}

The millis() function returns the number of
milliseconds since the Arduino board began
running the current program.
−This number will overflow (go back to zero), after
approximately 50 days.
−Syntax:
 millis()
−Parameters:
 None.
−Return:
 Number of milliseconds since the program started
(unsigned long)