Digital Outputs Push Button

Preview:

DESCRIPTION

Digital Outputs Push Button. Khaled A. Al- Utaibi alutaibi@uoh.edu.sa. 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

Digital OutputsPush ButtonKhaled A. Al-Utaibialutaibi@uoh.edu.sa

Agenda

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.

The 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

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

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

}

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

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

}

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

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)

// 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 }}

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

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.

Working with “Bouncy” ButtonsSwitch Debounce

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

// 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;}

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