Title: WAM
1WAM
- Ch3 Digital Input-Pushbuttons
2Activity 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.
3- Closed State With the button pressed, a
conductive material bridges the gap allowing
electrons, and thus current, to flow.
4Pushbutton Test Circuit
- This circuit demonstrates how the push-button
allows current to flow when closed.
Not pressed - Open No current flow, LED is
not-lit.
Pressed closed Current flows lighting the LED.
5Build this circuit
- You will need the following materials
- LEDPick a color
- Resistor470ohm
- Pushbuttonnormally open
- A jumper wire
6Test to see that your circuit is working correctly
- Supply power to the boe by positioning the switch
to 1 - When the button is not pressed, the LED should be
off - When the button is pressed, the LED should light
7- 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.
8Build this circuit
- You will need the same materials as before
9Repeat the same tests as before
- Explain why the opposite happens
- From the point of view of current taking the path
of least resistance?
10Activity 2 Reading a Pushbutton with the BS2
- Construct the circuit. Pay attention to the
values/colors of the resistors.
11Enter, Run and Save the code below
' STAMP BS2 ' PBASIC 2.5 'What's a
Microcontroller-ReadPushbuttonState.bs2 'Check
and send pushbutton state to Debug Terminal every
1/4 second. DO DEBUG ? IN3 PAUSE 250 LOOP
12- Test the program by occasionally pressing the
pushbutton and monitoring the state in the DEBUG
Window. If all goes right, you should see a
screen like the one shown below. - What is occuring if in3 0
- What is occuring if in3 1
13- When the switch is pressed, Vdd (5V) is sensed
at the input of P3 and a 1 is reported.
When the switch is released, Vss (0V) is sensed
at the input of P3 and a 0 is reported The 10K?
resistor prevents a short circuit from Vdd to Vss
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?
15Modify your circuit so it looks like the one below
- Run ReadPushbuttonState.bs2 again
- Verify that in3 is now 1 when the button is not
pressed. - Verify that in3 is now 0 when the button is
pressed
16ACTIVITY 3 Pushbutton Control of an LED circuit
- The BS2 can be programmed to make decisions based
on what it senses.
17Build the following circuits
- You will need
- Pushbutton (normally open)
- 10kohm resistor
- LED (choose a color)
- 220 ohm resistor
- 470 ohm resistor
- Jumper wires
18- Now that you can work with both outputs and
inputs, a pushbutton will be used as control for
an LED. - Pseudo-code
- If button is pressed
- Blink LED quickly at 20mSec
- Or else, if not pressed
- Keep LED off for 100mSec
- Loop back to Step 1
We know how to blink an LED On, pause, Off,
Pause.To reduce our design work, 'Blink' will
suffice.
19Enter,Run, and Save the following code
- Note, be sure to add the following to your code
- PushbuttonControlledLed.bs2
- Check pushbutton state 10 times per second and
blink LED when pressed
20- Save the program as pushbuttoncontrolledledmodifie
d1.bs2. Make this program flash the LED twice as
fast when you press and hold the button - Save the program as pushbuttoncontrolledledmodifie
d2.bs2. Make this program flash the LED half as
fast when you press and hold the button
- In the first case both pause durations should be
now 5 milliseconds - In the second case both pause durations should
now be 20 milliseconds
21Activity 4 2 Pushbuttons controlling 2 LED
circuits
- In this activity 2 buttons are used to control 2
LEDs. Build the circuits below
22- The last program utilized IFTHEN ELSE...
- We will now utilize
IF-THEN...ELSEIFELSE - This will allow us to have three possible
conditions - LED on pin14 lights
- LED on pin 15 lights
- Nothing lights
23- Enter, save, and run PushbuttonControlOfTwoLeds.bs
2 into the editior - Verify that the LED on pin 14 flashes on and off
while the pushbutton on pin 3 is pressed - Also, be sure that the LED on pin 15 flashes
while the button on pin 4 is pressed
24How pushbuttoncontroloftwoleds.bs2 works
- DEBUG home sends the cursor to the top left of
the debug terminal each cycle through the DOLOOP - ELSEIFallows the microprocessor to turn on the
other LED on pin 15 when in3 0 and in4 1 - ELSEallows action to be taken when both buttons
are low
25How pushbuttoncontroloftwoleds.bs2 workscontinued
- Note the two low commands appear after ENDIF
- You could add some more code so that the led is
turned off after it lights - OR since PBASIC commands execute so quickly, lets
just turn them both off at the same time this
will save us program space on in the eeprom
26PROBLEM
- Press both buttonsdo both LEDs flash
- NO
- Why?
- PBASIC executes commands in chronological order
- The action taken on the PIN 14 led will always
occur first even if both input pins are high
27To fix the problem
- Save pushbuttoncontroloftwoleds.bs2 under a new
name pushbuttoncontroloftwoledsmodified.bs2 - Replace the original if then with the following
- If (in3 1) and (in4 1) then
- High 14
- High 15
- Pause 50
- ELSEIF (in3 1) then
- High 14
- Pause 50
REMEMBER YOU CAN ADD AS MANY ELSEIFs AS YOU
LIKE.in this case our program has two ELSEIFswe
can now make 4 choices -light PIN14 LED, light
PIN 15 LED, light both LEDs, light neither
28(No Transcript)
29RUN THE PROGRAM
- VERIFY THAT YOUR PROGRAM WILL MAKE THE FOUR
CHOICES - light PIN14 LED, light PIN 15 LED, light both
LEDs, light neither
30Blink rates can also be modified
- Decrease the pause duration for both LEDs
blinking to 10 - Increase the pause duration for PIN 14 LED to 100
- Increase the pause duration for PIN 15 LED to 200
31Activity 5 Reaction Timer
- The Reaction Timer game tests how quickly a
person can react to the LED changing colors. - The player starts by pressing and holding a
button. This should make the LED turn red.
However, the player must let go of the button as
quickly as possible when the LED turns green.
The time is measured in milliseconds.
32Build the circuit below
Bi color LED
Run TestBiColorLED.bs2 from chapter 2 to test
the bicolor LED
33This program will utilize Polling, and
Counting
- Polling checking again and again quickly to
detect change - Counting adding a number to a variable each
time polling occurs
Polling will occur from the time the LED turns
green until the pushbutton is released. Each
poll will have a pause duration of 1ms Counting
will occur by adding 1 ms to the variable
timecounter each time the program polls When
the button is released, counting will cease, and
value of the timecounter variable will be
displayed on the DEBUG terminal
34Enter, Run, and Save ReactionTimer.bs2 on page 91
of your text. When running the program, follow
the prompts on the DEBUG terminal.
35(No Transcript)
36Lets discuss how this program works
- The time counter variable is declared and should
take up no more space than 255 bits - DEBUG sends the game instructions to the screen
37- Here we have a nested loop a loop in a loop
- The second do..loop uses a condition with UNTIL
to decide when to break out of this loop and move
on
38- These commands turn the LED red for about a
second then green
39Now the counting begins
- The value of the variable is set to zero
- Yet another nested doloop is started.ea
ch time the loop repeats 1ms is added to the
value of zero - Pause insures that this loop takes 1 ms to occur,
making the value of timecounter a number in
milliseconds - We break out of the loop when the button is
released - The led is turned offsame potential on both sides
40Display the results
- Note the presence of the commas, so that the
original DEBUG command applies to all - Without the DEC formatter, the value of
timecounter would correspond to a number in ascii - LOOPmakes the orginal loop start again
41New Objective Modify the existing program to
accommodate the following issues
- When a player holds the button for 30 seconds,
his score is actually 14000ms (14 seconds)
roughly ½ the actual. - The amount of time that it takes for the light to
turn green is always the samethis gives the
player an advantage - Letting go of the button before the light turns
green gives the player a score of 1ms!!
42Fixing problem 1
- When a player holds the button for 30 seconds,
his score is actually 14000ms (14 seconds)
roughly ½ the actual.
- The code overhead or time for the program to
execute commands takes about 1 ms - So 1ms 1ms 2ms
- Or 14000ms 14000 ms 28000 ms roughly 30
seconds - Fix by eliminating pause1 or by commenting it out
43Also try the following
- Leave the program alone, and simply multiply the
value of timecounter by 2 in the end - TRY ALL THREE OF THESE AND SEE IF IT FIXES THE
PROBLEM
44Fixing problem 2
- The amount of time that it takes for the light to
turn green is always the samethis gives the
player an advantage
- You will need to know about two new items to fix
this - RANDOM a command that changes the value of a
variable each time it is executed in a loop - Syntax
- RANDOM variable
- A seeda number which executes a specific random
number selecting sequence
45Fixing problem 2 contd.
- In the beginning of your code add
- Value var byte this is the variable that will
be randomized - Value 23 this is the seed
- Before the pause command, use the RANDOM command
to give value a new random value from seed
sequence 23add the following - RANDOM value
- DEBUG Delay time , ? 1000 value,CR
- Change the pause command so that value is added
to 1000ms (1 second)add the following - PAUSE 1000 value
- Run the program, and see if this fixes the
problemWHAT IS THE SIGNIFICANCE OF DELAY TIME
1000 value ON THE DEBUG TERMINAL?
46Fixing problem 3
- Letting go of the button before the light turns
green gives the player a score of 1ms!!
- This will be fixed by
- IF value of timecounter 1
- Display a message telling the player he or she
has to wait until after the light turns green to
let go of the button - ELSE
- Display the value of timecounter
47Fixing problem 3Contd.
- Add the program suggestions on the previous slide
- Run the program to see if this fixes the problem
48Side note on IF, THEN,ELSE
- It is desired to have the basic stamp report when
either of two active high buttons are on or
high. The reporting should end when both go low.
49This will not work
50This wont work either
51Neither will this
52We have a winner!!
53Project 1
- Modify ReactionTimer.bs2 so that it is a two
player game. HINTS Use the AND operator in your
DOLOOP UNTIL loop. Declare two different
timeCounter variables, timeCounterA and
timeCounterB. Use two IFTHEN statements in the
DOLOOP that increments the counters. Be sure
that the three limitations of the original
reaction timer are corrected. Connect the second
button to pin 4 and make it active high.
54Project 2
- Make ReactionTimer.bs2 or TwoPlayerReactionTimer.b
s2 work with DO WHILE instead of LOOP UNTIL - Make ReactionTimer.bs2 or Two PlayerReaction
Timer.bs2 work with DO LOOP WHILE instead of DO
WHILE OR LOOP UNTIL - HINT OR can be used in place of AND
55Graded Project
- Some streetlight controllers do not work on just
a timer they have sensors under the pavement
that detect whether or not a car is waiting at a
red light. Make a traffic light controller so
the north-south street gets a green light all the
time, but a press on the pushbutton will
initialize the east-west sequence to let a car
through. After the east-west traffic light goes
through one cycle, North-South should go back to
green. At this point polling for east-west
traffic should begin again.
- Conditions
- Light Transitions
- Green 3 seconds
- Yellow 1 seconds
- Both must be red for 1 seconds before allowing a
green in the other direction - If the sensor detects a car during each phase of
the east west cycle (G-Y-R), North-South should
become green again, but east-west should blink
yellow once every second - Normal cycle will begin once the stalled car has
been removed from blocking the sensor
56Other Projects
- Blink LED P0 on and off every ½ sec as long as
switch P2 is pressed. When the button is not
pressed, LED P1 is on, but goes off when LED P0
is flashing. - Blink two LEDs every 1.2 seconds when either
switch is pressed. If no switches are pressed,
then LEDs are on and if both switches are pressed
both LEDs are off.
57Do Now 1
- Use Active Low to make p15 led blink on for 60 ms
then off for 60 ms when button is pressed, but
blink on for 10 ms then off for 10 ms when button
is not pressed
58Do Now 2
- An active high is connected to pin 3, and an
active low is connected to pin 4. A green led is
connected to pin 14, and a red led is connected
to pin 15. - Write a program that blinks green every second
when p3 button is pressed, red every second when
p4 button is pressed, and blinks alternately red
for ½ second then green for ½ second when both
are pressed.