0% found this document useful (0 votes)
63 views

Button Input: On/off State Change: User Input Features of The Fan

The document discusses different methods for reading button input on an Arduino board, including continuously monitoring the button state, waiting for button input, and using an interrupt handler. It describes the circuit diagram for connecting a button to read input and provides code examples for each of the button reading methods. The interrupt handler approach is highlighted as the most versatile method that does not block execution and uses a flag to indicate button state changes in the interrupt service routine.

Uploaded by

RKJMM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Button Input: On/off State Change: User Input Features of The Fan

The document discusses different methods for reading button input on an Arduino board, including continuously monitoring the button state, waiting for button input, and using an interrupt handler. It describes the circuit diagram for connecting a button to read input and provides code examples for each of the button reading methods. The interrupt handler approach is highlighted as the most versatile method that does not block execution and uses a flag to indicate button state changes in the interrupt service routine.

Uploaded by

RKJMM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Button Input:

On/off state change


Desktop fan project
EAS 199A, Fall 2011
EAS 199A: Button Input
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
2
EAS 199A: Button Input
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
3
Image from lowes.com Image from sparkfun.com
EAS 199A: Button Input
!"# %"&' ( )*+", #"-./
01234& '#1567 '67&2(86
9'& :;; 5" 2&('*-& "3&,<64"'&% 61-6*15
;(3 57& 31, '5(5&'
EAS 199A: Button Input
;&('*-& =3&, (,% >4"'&% >1-6*15'
;&('*-&% ?&'1'5(,6& @AB ;&('*-&% ?&'1'5(,6& @AB
>",,&65
C1,'
D7&, ,"5
3-&''&% D7&, 3-&''&%
E (,% F
E (,% G
E (,% H
F (,% G
EAS 199A: Button Input
;&('*-& =3&, (,% >4"'&% >1-6*15'
;&('*-&% ?&'1'5(,6& @AB ;&('*-&% ?&'1'5(,6& @AB
>",,&65
C1,'
D7&, ,"5
3-&''&% D7&, 3-&''&%
E (,% F
E (,% G
E (,% H
F (,% G
0.&567 >",,&68",'I :(5( J-"2 ;&('*-&2&,5'I
EAS 199A: Button Input
C*'7 K*+", 0#1567&'
L 2"2&,5(-M )*+", 1' ( NK1('&% 0#1567O
C*'71,P 57& )*+", 67(,P&' '5(5&
05(5& 1' -&Q&-'&% @-&5*-, 5" )1('&% 3"'18",B #7&, )*+", 1'
-&4&('&%
R#" 5M3&'
!S=I ,"-2(44M "3&,
!S>I ,"-2(44M 64"'&%
Normally Open Normally Closed
EAS 199A: Button Input
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
8
Image from sparkfun.com
Open Closed
EAS 199A: Button Input
Putting buttons into action
1. Build the circuit: same one is used for all examples
Test with LED on/off
LED is only controlled by the button, not by Arduino code
2. Create a wait to start button
Simplest button implementation
Execution is blocked while waiting for a button click
3. Use an interrupt handler
Most sophisticated: Dont block execution while waiting for
button input
Most sophisticated: Requires good understanding of coding
Requires de-bouncing
Not too hard to use as a black box
9
EAS 199A: Button Input
Digital input with a pull-down resistor
!
When switch is open (button not pressed):
! Digital input pin is tied to ground
! No current ows, 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 ows 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
10
LED
10 k
330
5V
Digital
input pin
Push-button
switch
EAS 199A: Button Input
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
All three programs use the same electrical circuit
11
EAS 199A: Button Input
Continuous monitor of button state
12
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
LED
10 k
330
5V
Digital
input pin
Push-button
switch
EAS 199A: Button Input
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
All three programs use the same electrical circuit
13
EAS 199A: Button Input
Wait for button input
14
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
before
while loop continues
as long as start_click
is FALSE
LED
10 k
330
5V
Digital
input pin
Push-button
switch
EAS 199A: Button Input
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 ag that indicates state
!
Regular code in loop function checks the sate of the ag
All three programs use the same electrical circuit
15
EAS 199A: Button Input
Interrupt handler for button input
16
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;
}
LED
10 k
330
5V
Digital
input pin
Push-button
switch
EAS 199A: Button Input
Interrupt handler for button input
17
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
EAS 199A: Button Input
Interrupt handler for button input
18
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 ipped only
when a true interrupt even occurs. De-
bouncing is described in the next slide.
EAS 199A: Button Input
Interrupt handler for button input
19
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
EAS 199A: Button Input
Other references
Ladyada tutorial
!
Excellent and detailed
!
http://www.ladyada.net/learn/arduino/lesson5.html
Arduino reference
!
Minimal explanation
! http://www.arduino.cc/en/Tutorial/Button
!
Using interrupts
! http://www.uchobby.com/index.php/2007/11/24/arduino-interrupts/
! http://www.arduino.cc/en/Reference/AttachInterrupt
20

You might also like