wam_ch3
wam_ch3
Pushbuttons
1
Presentation Index
Use and Copyright
Pushbuttons
Activity #1: Testing a Pushbutton/LED
Circuit
Activity #2: Reading a Pushbutton
Activity #3: Pushbutton Controlled LED
Activity #4: 2 Pushbuttons, 2 LEDs
Logical Operators – AND, OR, XOR
Activity #5: Reaction Timer
Using the PIN and CON commands
Real World Testing
Chapter 3 Review
Links
2
Use and Copyright
This presentation supplements "What's a
Microcontroller" by Andy Lindsay. (Link to text at
Parallax)
This presentation is not a replacement for the text.
Important concepts of the text are highlighted.
In some cases, additional material has been added
to augment the text. Denoted by titles colored
gold.
gold
Full program listings are generally not provided in
the presentation.
Distribution:
This presentation may be freely distributed without
modifications. Modifications are permitted by
schools and organizations for internal use only.
Credits, use and copyright slides must remain.
3
COPYRIGHTS AND TRADEMARKS
This documentation is Copyright 2003 by Parallax, Inc. By downloading or
obtaining a printed copy of this documentation or software you agree that it
is to be used exclusively with Parallax products. Any other uses are not
permitted and may represent a violation of Parallax copyrights, legally
punishable according to Federal copyright or intellectual property laws. Any
duplication of this documentation for commercial uses is expressly
prohibited by Parallax, Inc. Check with Parallax for approval prior to
duplicating any of our documentation in part or whole for any use.
BASIC Stamp is a registered trademark of Parallax, Inc. If you decide to use the
name BASIC Stamp on your web page or in printed material, you must state
that "BASIC Stamp is a registered trademark of Parallax, Inc." Other brand
and product names are trademarks or registered trademarks of their
respective holders.
DISCLAIMER OF LIABILITY
Parallax, Inc. and Southern Illinois University are not responsible for special,
incidental, or consequential damages resulting from any breach of
warranty, or under any legal theory, including lost profits, downtime,
goodwill, damage to or replacement of equipment or property, or any costs
of recovering, reprogramming, or reproducing any data stored in or used
with Parallax products. Parallax is also not responsible for any personal
damage, including that to life and health, resulting from use of any of our
products. You take full responsibility for your BASIC Stamp application, no
matter how life threatening it may be.
4
Pushbuttons
Pushbuttons are virtually everywhere
interactions with an electronics
device are required.
5
Activity #1: Testing a PushButton/LED
Circuit
The pushbuttons supplied with the kits are
normally-open, momentary contact.
That is, the switch does not make contact
until the button is pressed. Once released,
it returns to the open position.
Open State: The pins on either side are
electrically the same point. With the
button released, there is no path for
electrons between pins 1,4 and 2,3.
6
Closed State: With the button
pressed, a conductive material
bridges the gap allowing electrons,
and thus current, to flow.
7
Pushbutton Test Circuit
This circuit demonstrates how the
push-buttons switch allows current
to flow when closed.
Not pressed -
Open: No
current flow, LED
is not-lit.
Pressed –
Open: Current
flows lighting the
LED.
8
This circuit demonstrates how the
switch can create a short-circuit
around the LED. Current will take
the easiest path and not flow
through the LED.
Shorts are
usually not
desirable. Note
that resistor is
still in the path
either way to
ensure excessive
current is not
drawn.
9
Activity #2: Reading a Pushbutton
Construct the circuit. Pay attention
to the values/colors of the resistors.
10
Enter and test the code by
occasionally pressing the pushbutton
and monitoring the state in the
DEBUG Window.
11
DEBUG ? IN3 displays the value of I/O
P3 in the DEBUG Window. Which
state relates to 1? Pressed or not
pressed?
12
When the switch is
pressed, Vdd
(+5V) is sensed at
the input of P3.
14
This configuration shows a Pull-Up
resistor to Vdd, with an Active-Low
button.
When the same code is ran with this
configuration, when will IN3 be a
value of 1?
15
A BASIC Stamp input must always be
pulled high or low. If not connected to
either, it is said to be floating and
produce erratic readings as voltages
at the pin fluctuate around 1.4V.
<1.4V = Low
>1.4V = High
16
Activity #3: Pushbutton Controlled
LED
Now that you can work with both
outputs and inputs, a pushbutton
will be used as control for an LED.
We know
how to blink
Pseudo-code: an LED:
On, pause,
1. If button is pressed: Off, Pause.
To reduce
• Blink LED quickly at 20mSec our design
work, 'Blink'
2. Or else, if not pressed: will suffice.
A A
18
Code for Pushbutton controlled LED Control:
19
The IF…THEN…ELSE is a decision
making structure.
IF (condition) THEN
code
ENDIF
IF (condition) THEN
code
ELSEIF (condition) THEN
code
ENDIF
21
Activity #4: 2 Pushbuttons, 2 LEDs
In this activity 2 buttons are used to
control 2 LEDs.
22
Fragments of decision code and flow :
F PB on 3 T
Pressed
LED on
14 ON
F PB on 4 T
Pressed Pause
PAUSE LED on
15 ON B
B Pause
24
AND: BOTH conditions have to be true
for the overall statement to be true.
It needs this AND that to be true.
1st : X is 4 or 5
2nd: X is 0,1,2,3 or 9,10,11….
26
For the LED
control, logical
operators can be
used to make
both LED's
operate when
both buttons are
pressed.
27
Activity #5: Reaction Timer
The Reaction Timer game tests how
quickly a person can react to the
LED changing colors.
28
29
The game brings out some real world
examples of problems involved.
31
The RANDOM instruction
'randomizes' the value of the
variable.
34
PIN is used to name the I/O:
LED_Green PIN 14
LED_RED PIN 15
PB1 PIN 3
PB2 PIN 4
35
The code becomes so
readable that it greatly
reduces the need for
comments to
understand what is
being performed.
36
Real World Testing
Real world use of a product requires
careful testing to ensure it is
accurate and operates correctly
under ALL circumstances.
37
The pushbutton switch is only one of
many devices that can be read as a
digital inputs. You will come across
many more in your explorations with
the BASIC Stamp and electronics.
38
Chapter 3 Review
What electronic action does a switch
perform?
What is meant by: Active-High? Active-Low?
What command is used to read the state of
an input?
What command structure is used to make
decisions?
AND, OR, XOR are ________ operators. What
does each require to be true?
What does the RANDOM command do?
What is meant by the seed value?
____ and ____ can greatly increase code
readability.
Why does real-world use requires extensive
testing.
39
Links
BASIC Stamp Home
Stamps In Class Home
BASIC Stamp Software
BASIC Stamp Robots
BASIC Stamp Yahoo Group
Stamps In Class Yahoo Group
SIUC EST Degree
40