Button Input: On/off state change Living with the Lab Gerald Recktenwald Portland State University

Slides:



Advertisements
Similar presentations
555 Timer 555 Timer Digital Electronics TM 1.2 Introduction to Analog
Advertisements

Wireless Cue Light Project
Book Project Sue Brandt Arduino Project Interactive with LEDS.
As a stand-alone overview of test tools.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
0 - 0.
Addition Facts
Sequential Process Control
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
John Ogilvie High School - CfE Physics
Khaled A. Al-Utaibi Interfacing an LED The Light Emitting Diode (LED) Applications DC Characteristics & Operation Interfacing to.
Red Team Final Presentation December 8, Fire Safe Microwave 2.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
EMS1EP Lecture 6 Digital Inputs
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
EMS1EP Lecture 8 Pulse Width Modulation (PWM)
Control Structures Selections Repetitions/iterations
Addition 1’s to 20.
25 seconds left…...
ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions.
Week 1.
10 Ways to Ruin Your Mini-Max microcontroller (and lose $69)
Lab 9: Matrix Keypad : ”No Key Press” Analysis Slide #2 Slide #3 ”Press and Hold Key 5” Analysis.
Lab7: Introduction to Arduino
Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
How to use Arduino By: Andrew Hoffmaster.
Embedded Sumo 1T4 – 1T5 UTRA.
Basic DC Motor Circuits
Panasonic EVE-KC2F2024B 24 pulses per revolution 6mm diameter flattened output shaft output type: quadrature (incremental) minimum life: 15,000 rotations.
Digital & Analog Inputs. Review Fundamental parts of an Arduino program are … Setting output types using pinMode. Declaring variables Can write a digital.
Debouncing Switches Mechanical switches are one of the most common interfaces to a uC. Switch inputs are asynchronous to the uC and are not electrically.
Analog and Digital Measurements living with the lab 14 digital input / output pins 6 analog input pins © 2011 LWTL faculty team.
Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011.
Embedded Programming and Robotics
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
chipKit Sense Switch & Control LED
Switches & whiskers on the Arduino living with the lab lever arm switches mounted to Arduino © 2012 David Hall.
Khaled A. Al-Utaibi  The Push Button  Interfacing Push Buttons to Arduino  Programming Digital Inputs  Working with “Bouncy”
Engineering 1040: Mechanisms & Electric Circuits Winter 2015 Interfacing Light-Emitting Diodes (LEDs) & Push Buttons to Microcontrollers.
Code The Arduino Environment.
INTERNET OF EVERYTHING SDU 2016 Week 4. Simple Digital and Analog Inputs  The Arduino’s ability to sense digital and analog inputs allows it to respond.
Photoresistor resistance changes dramatically with light level living with the lab Using Photoresistors with an Arduino © 2011 LWTL faculty team.
Embedded Programming and Robotics Lesson 11 Arduino Interrupts 1.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
Arduino “Getting Started” Instructor : Dr Matthew Miss Khin Yi Kyaw
Arduino Application: Speed control of small DC Motors
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Prototyping with Microcontrollers and Sensors. Overview Objective Background Information Problem Statement Materials Procedure Assignment Closing.
Arduino.
Lab 7 Basic 1: Game of Memory
Assist. Prof. Rassim Suliyev - SDU 2017
Prototyping with Microcontrollers and Sensors
More on LED’s with Arduino
Microcontroller basics
European Robotic LABoratory
Programming Concepts (Part B) ENGR 10 Introduction to Engineering
How to avoid catching things on fire.
ARDUINO     What is an Arduino? Features 14 Digital I/O pins 6 Analogue inputs 6 PWM pins USB serial 16MHz Clock speed 32KB Flash memory 2KB SRAM.
Arduino Application: Speed control of small DC Motors
IoT Programming the Particle Photon.
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
CS-4540 Robotics - Lab 05 Switch inputs to the Arduino Uno
Implementing Switches Using Interrupts
Arduino programs Arduino toolchain Cross-compilation Arduino sketches
Interfacing a Rotary Encoder with an Arduino
CTY SAR FCPS Shawn Lupoli, Elliot Tan
Digital INPUTS/OUTPUTS and interrupts
Arduino Uno circuit basics
Interrupts.
Presentation transcript:

Button Input: On/off state change Living with the Lab Gerald Recktenwald Portland State University

LWTL: Button Input 2 User input features of the fan Potentiometer for speed control ❖ Continually variable input makes sense for speed control ❖ Previously discussed Start/stop ❖ Could use a conventional power switch ❖ Push button (momentary) switch Lock or limit rotation angle ❖ Button click to hold/release fan in one position ❖ Potentiometer to set range limit

LWTL: Button Input 3 Conventional on/off switch Basic light switch or rocker switch ❖ Makes or breaks connection to power ❖ Switch stays in position: On or Off ❖ Toggle position indicates the state ❖ NOT in the Arduino Inventors Kit Image from lowes.com Image from sparkfun.com

LWTL: Button Input How does a button work? Simple switch schematic Use DMM to measure open/closed circuit Map the pin states

LWTL: Button Input Measure Open and Closed Circuits Measured Resistance (Ω) Connect Pins When not pressedWhen pressed 1 and 2 1 and 3 1 and 4 2 and 3

LWTL: Button Input Measure Open and Closed Circuits Measured Resistance (Ω) Connect Pins When not pressedWhen pressed 1 and 2 1 and 3 1 and 4 2 and 3 Sketch Connections: Data from Measurements:

LWTL: Button Input Push Button Switches A momentary button is a “ Biased Switch ” Pushing the button changes state State is reversed (return to biased position) when button is released Two types NO: normally open NC: normally closed

LWTL: Button Input 8 Normally open ❖ electrical contact is made when button is pressed Normally closed ❖ electrical contact is broken when button is pressed Internal spring returns button to its un-pressed state Momentary or push-button switches Image from sparkfun.com

LWTL: Button Input 9 Putting buttons into action 1.Build the circuit: same one is used for all examples a. Test with LED on/off b. LED is only controlled by the button, not by Arduino code 2.Create a “wait to start” button a. Simplest button implementation b. Execution is blocked while waiting for a button click 3.Use an interrupt handler a. Most sophisticated: Don’t block execution while waiting for button input b. Most sophisticated: Requires good understanding of coding c. Requires “de-bouncing” d. Not too hard to use as a black box

LWTL: Button Input 10 Digital input with a pull-down resistor ❖ When switch is open (button not pressed): ‣ Digital input pin is tied to ground ‣ No current flows, so there is no voltage difference from input pin to ground ‣ Reading on digital input is LOW ❖ When switch is closed (button is pressed): ‣ Current flows from 5V to ground, causing LED to light up. ‣ The 10k resistor limits the current draw by the input pin. ‣ The 330Ω resistor causes a large voltage drop between 5V and ground, which causes the digital input pin to be closer to 5V. ‣ Reading on digital input is HIGH Momentary Button and LED Circuit

LWTL: Button Input 11 Technical Note Usually we do not include an LED directly in the button circuit. The following diagrams show plan button circuits with pull-up and pull-down resistors. In these applications, the pull-up or pull-down resistors should be 10k. Refer to Lady Ada Tutorial #5: ❖ Pull-up resistor: Pull-down resistor:

LWTL: Button Input 12 Programs for the LED/Button Circuit 1.Continuous monitor of button state ❖ Program is completely occupied by monitoring the button ❖ Used as a demonstration — not practically useful 2.Wait for button input 3.Interrupt Handler 4.All three programs use the same electrical circuit

LWTL: Button Input 13 Continuous monitor of button state int button_pin = 4; // pin used to read the button void setup() { pinMode( button_pin, INPUT); Serial.begin(9600); // Button state is sent to host } void loop() { int button; button = digitalRead( button_pin ); if ( button == HIGH ) { Serial.println("on"); } else { Serial.println("off"); } Serial monitor shows a continuous stream of “on” or “off” This program does not control the LED

LWTL: Button Input 14 Programs for the LED/Button Circuit 1.Continuous monitor of button state ❖ Program is completely occupied by monitoring the button ❖ Used as a demonstration — not practically useful 2.Wait for button input ❖ Blocks execution while waiting ❖ May be useful as a start button 3.Interrupt Handler 4.All three programs use the same electrical circuit

LWTL: Button Input 15 Wait for button input int button_pin = 4; // pin used to read the button void setup() { int start_click = LOW; // Initial state: no click yet pinMode( button_pin, INPUT); Serial.begin(9600); while ( !start_click ) { start_click = digitalRead( button_pin ); Serial.println("Waiting for button press"); } void loop() { int button; button = digitalRead( button_pin ); if ( button == HIGH ) { Serial.println("on"); } else { Serial.println("off"); } Same loop() function as in the preceding sketch while loop continues as long as start_click is FALSE

LWTL: Button Input 16 Programs for the LED/Button Circuit 1.Continuous monitor of button state ❖ Program is completely occupied by monitoring the button ❖ Used as a demonstration — not practically useful 2.Wait for button input ❖ Blocks execution while waiting ❖ May be useful as a start button 3.Interrupt Handler ❖ Most versatile ❖ Does not block execution ❖ Interrupt is used to change a flag that indicates state ❖ Regular code in loop function checks the sate of the flag 4.All three programs use the same electrical circuit

LWTL: Button Input 17 Interrupt handler for button input int button_interrupt = 0; // Interrupt 0 is on pin 2 !! int toggle_on = false; // Button click switches state void setup() { Serial.begin(9600); attachInterrupt( button_interrupt, handle_click, RISING); // Register handler } void loop() { if ( toggle_on ) { Serial.println("on"); } else { Serial.println("off"); } void handle_click() { static unsigned long last_interrupt_time = 0; // Zero only at start unsigned long interrupt_time = millis(); // Read the clock if ( interrupt_time - last_interrupt_time > 200 ) { // Ignore when < 200 msec toggle_on = !toggle_on; } last_interrupt_time = interrupt_time; }

LWTL: Button Input 18 Interrupt handler for button input int button_interrupt = 0; // Interrupt 0 is on pin 2 !! int toggle_on = false; // Button click switches state void setup() { Serial.begin(9600); attachInterrupt( button_interrupt, handle_click, RISING); // Register handler } void loop() { if ( toggle_on ) { Serial.println("on"); } else { Serial.println("off"); } void handle_click() { static unsigned long last_interrupt_time = 0; // Zero only at start unsigned long interrupt_time = millis(); // Read the clock if ( interrupt_time - last_interrupt_time > 200 ) { // Ignore when < 200 msec toggle_on = !toggle_on; } last_interrupt_time = interrupt_time; } Interrupt handler must be registered when program starts The interrupt handler, handle_click, is a user-written function that is called when an interrupt is detected button_interrupt is the ID or number of the interrupt. It must be 0 or 1 A RISING interrupt occurs when the pin changes from LOW to HIGH

LWTL: Button Input 19 Interrupt handler for button input int button_interrupt = 0; // Interrupt 0 is on pin 2 !! int toggle_on = false; // Button click switches state void setup() { Serial.begin(9600); attachInterrupt( button_interrupt, handle_click, RISING); // Register handler } void loop() { if ( toggle_on ) { Serial.println("on"); } else { Serial.println("off"); } void handle_click() { static unsigned long last_interrupt_time = 0; // Zero only at start unsigned long interrupt_time = millis(); // Read the clock if ( interrupt_time - last_interrupt_time > 200 ) { // Ignore when < 200 msec toggle_on = !toggle_on; } last_interrupt_time = interrupt_time; } toggle_on is a global variable that remembers the “state”. It is either true or false (1 or 0). The loop() function only checks the state of toggle_on. The value of toggle_on is set in the interrupt handler, handle_click. The value of toggle_on is flipped only when a true interrupt even occurs. De- bouncing is described in the next slide.

LWTL: Button Input 20 Interrupt handler for button input int button_interrupt = 0; // Interrupt 0 is on pin 2 !! int toggle_on = false; // Button click switches state void setup() { Serial.begin(9600); attachInterrupt( button_interrupt, handle_click, RISING); // Register handler } void loop() { if ( toggle_on ) { Serial.println("on"); } else { Serial.println("off"); } void handle_click() { static unsigned long last_interrupt_time = 0; // Zero only at start unsigned long interrupt_time = millis(); // Read the clock if ( interrupt_time - last_interrupt_time > 200 ) { // Ignore when < 200 msec toggle_on = !toggle_on; } last_interrupt_time = interrupt_time; } Value of a static variable is always retained Ignore events that occur in less than 200 msec from each other. These are likely to be mechanical bounces. Use long: the time value in milliseconds can become large Clock time when current interrupt occurs Save current time as the new “last” time

LWTL: Button Input 21 Other references Ladyada tutorial ❖ Excellent and detailed ❖ Arduino reference ❖ Minimal explanation ‣ ❖ Using interrupts ‣ ‣