1 / 19

Digital Outputs Push Button

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.

august
Download Presentation

Digital Outputs Push Button

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Digital OutputsPush Button Khaled A. Al-Utaibi alutaibi@uoh.edu.sa

  2. Agenda • The Push Button • Interfacing Push Buttons to Arduino • Programming Digital Inputs • Working with “Bouncy” Buttons

  3. The 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.

  4. Interfacing Push Buttons to Arduino • 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).

  5. Programming Digital Inputs • 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.

  6. Programming Digital InputsConfiguring 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. // set pin 4 as a digital input void setup() { pinMode(9, INPUT); }

  7. Programming Digital InputsThe pinMode Function • 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

  8. Programming Digital InputsReading Digital Inputs • Because the loop() function runs over and over again, inputs are usually read in this function. // read the state of pin 4 intpinState; void loop() { pinState = digitalRead(4); }

  9. Programming Digital InputsThe digitalRead Function • 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

  10. Programming Digital InputsExample (Reading Button State) • 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.

  11. // Repeatedly Reads the state of a bush button connected to pin 4, // and displays the message “Button Pressed” whenever the button // is pushed. intbuttonState;// 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 }}

  12. Working with “Bouncy” ButtonsSwitch Bounce • 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.

  13. Ideal Switch (No Bounce) Switch Bounce

  14. Working with “Bouncy” ButtonsSwitch Debounce • 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.

  15. Working with “Bouncy” ButtonsSoftware Switch 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.

  16. // Modify previous program to debounce the switchintbuttonState; // the current reading from the input pinintpreviousButtonState = LOW; // the previous reading from the input pin longlastDebounceTime = 0;   // the last time the output pin was toggledlongdebounceDelay = 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 timerlastDebounceTime = millis();  } if ((millis() - lastDebounceTime) > debounceDelay) {// the reading has been there for longer than the debounce delay, so take itif (reading != buttonState) {buttonState = reading;if (buttonState == HIGH) { // if pin state is HIGH Serial.println(“Button Pressed”); // display the message } }  } // save the readinglastButtonState = reading;}

  17. Programming Digital InputsThe millis()Function • 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)

More Related