19
Digital Outputs Push Button Khaled A. Al-Utaibi [email protected]

Digital Outputs Push Button

  • Upload
    august

  • View
    59

  • Download
    3

Embed Size (px)

DESCRIPTION

Digital Outputs Push Button. Khaled A. Al- Utaibi [email protected]. Agenda. The Push Button Interfacing Push Buttons to Arduino Programming Digital Inputs Working with “Bouncy” Buttons. The Push Button. The simplest form of digital input is a push button. - PowerPoint PPT Presentation

Citation preview

Page 1: Digital Outputs Push Button

Digital OutputsPush ButtonKhaled A. [email protected]

Page 2: Digital Outputs Push Button

Agenda

The Push Button

Interfacing Push Buttons to Arduino

Programming Digital Inputs

Working with “Bouncy” Buttons

Page 3: Digital Outputs Push Button

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.

The Push Button

Page 4: Digital Outputs Push Button
Page 5: Digital Outputs Push Button

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).

Interfacing Push Buttons to Arduino

Page 6: Digital Outputs Push Button
Page 7: Digital Outputs Push Button

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.

Programming Digital Inputs

Page 8: Digital Outputs Push Button

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.

Programming Digital InputsConfiguring Digital Inputs

// set pin 4 as a digital input

void setup() {

  pinMode(9, INPUT);

}

Page 9: Digital Outputs Push Button

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

Programming Digital InputsThe pinMode Function

Page 10: Digital Outputs Push Button

Because the loop() function runs over and over again, inputs are usually read in this function.

Programming Digital InputsReading Digital Inputs

// read the state of pin 4

int pinState;

void loop() {

  pinState = digitalRead(4);

}

Page 11: Digital Outputs Push Button

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

Programming Digital InputsThe digitalRead Function

Page 12: Digital Outputs Push Button

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.

Programming Digital InputsExample (Reading Button State)

Page 13: Digital Outputs Push Button

// 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, OUTPUT); // 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 }}

Page 14: Digital Outputs Push Button

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.

Working with “Bouncy” ButtonsSwitch Bounce

Page 15: Digital Outputs Push Button

Ideal Switch (No Bounce) Switch Bounce

Page 16: Digital Outputs Push Button

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.

Working with “Bouncy” ButtonsSwitch Debounce

Page 17: Digital Outputs Push Button

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.

Working with “Bouncy” ButtonsSoftware Switch Debounce

Page 18: Digital Outputs Push Button

// Modify previous program to debounce the switchint buttonState; // the current reading from the input pinint previousButtonState = LOW; // the previous reading from the input pinlong lastDebounceTime = 0;   // the last time the output pin was toggledlong 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;}

Page 19: Digital Outputs Push Button

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)

Programming Digital InputsThe millis() Function