410 likes | 895 Views
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.
E N D
Digital OutputsPush 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. • 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.
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).
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.
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); }
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
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); }
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
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.
// 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 }}
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.
Ideal Switch (No Bounce) Switch Bounce
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.
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.
// 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;}
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)