Quantcast
Channel: Learn with BlocklyProp — Parallax Forums
Viewing all articles
Browse latest Browse all 293

Build Your Own Mini Timer

$
0
0
Build Your Own Mini Timer
·
Timers are extremely useful in everyday life.· We use them to cook, play games, and even when teachers give tests.· This activity will show you how to build your own 1-9 minute timer using only parts included in the BASIC Stamp Activity Kit.

View Video Introduction (YouTube)
Download Source Code

Getting Started

If you are new to the BASIC Stamp microcontroller or to programming, it would be a good idea to review the following before continuing:
·
√······· Complete the following chapters in What’s a Microcontroller:
o······· Chapter 1 (Getting Started with the BASIC Stamp)
o······· Chapter 3, Activities 1 & 2 (Pushbutton Control)
o······· Chapter 5, Activity 1-3 (Measuring Rotation)
o······· Chapter 6 (Digital Display)
o······· Chapter 8 (Frequency and Sound)
√······· Review the following commands in the BASIC Stamp Syntax and Reference Manual (page numbers from v2.2):
o········ LOOKDOWN (page 271)
o········ LOOKUP (page 277)

Parts Required

This activity can be completed solely with the parts included in the BASIC Stamp Activity Kit or the Radio Shack What's a Microcontroller BASIC Stamp Kit.· Below is a listing of all parts required for this activity:

(1)· HomeWork Board with BASIC Stamp 2
····· The BASIC Stamp 2 Board of Education is also suitable for·this activity
(1)· 7-Segment LED display
(1)· Piezospeaker
(1)· 10 k Ω potentiometer
(1)· Pushbutton
(2)· 220 Ω Resistors
(8)· 1 k Ω Resistors
(1)· 10 k Ω Resistor

Schematics and Building the Circuits

Not only does this activity provide another useful application for your WAM Kit, but it is also a great lesson on optimizing your breadboard real estate.· There are a lot of components to fit onto the board, so we need to make the most of the space available.· Figure 1 shows the schematic and Figure 2 shows the wiring diagram for this mini timer.

Figure 1 – Timer Circuit Schematic
attachment.php?attachmentid=61160

Figure 2 – Timer Circuit Wiring Diagram
attachment.php?attachmentid=61161

Testing the Circuit

Once everything is wired, it’s important to check that everything is wired correctly.· The program below will test each aspect of the circuit in one shot.· Run TestCircuit.bs2 and verify that the following occurs:

√······· The piezospeaker plays a tone
√······· The 7-Segment LED displays the numbers 0-9
√······· The decay time of the potentiometer ranges from about 1-600
√······· The pushbutton state is 1 when the button is pressed and 0 when the button is not pressed.
[color=#008000]' TestCircuit.bs2[/color]
[color=#008000]' Test each electrical component to be sure things were wired correctly.[/color]
 
[color=#008000]' {$STAMP BS2}[/color]
[color=#008000]' {$PBASIC 2.5}[/color]
 
[color=#000000]time     VAR    Word                      [/color][color=#008000]' Variable space for decay time[/color]
 
[color=#800080]OUTH[/color][color=#000000] = %00000000                          [/color][color=#008000]' Clear LED display[/color]
[color=#800080]DIRH[/color][color=#000000] = %11111111                          [/color][color=#008000]' Set pins 8-15 to outputs[/color]
 
[color=#020FC0]FREQOUT[/color][color=#000000] 7, 100, 3500                      [/color][color=#008000]' Test speaker[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 100[/color]
[color=#020FC0]FREQOUT[/color][color=#000000] 7, 100, 3500[/color]
 
[color=#800080]OUTH[/color][color=#000000] = %11100111                          [/color][color=#008000]' 0[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 500[/color]
[color=#800080]OUTH[/color][color=#000000] = %10000100                          [/color][color=#008000]' 1[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 500[/color]
[color=#800080]OUTH[/color][color=#000000] = %11010011                          [/color][color=#008000]' 2[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 500[/color]
[color=#800080]OUTH[/color][color=#000000] = %11010110                          [/color][color=#008000]' 3[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 500[/color]
[color=#800080]OUTH[/color][color=#000000] = %10110100                          [/color][color=#008000]' 4[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 500[/color]
[color=#800080]OUTH[/color][color=#000000] = %01110110                          [/color][color=#008000]' 5[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 500[/color]
[color=#800080]OUTH[/color][color=#000000] = %01110111                          [/color][color=#008000]' 6[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 500[/color]
[color=#800080]OUTH[/color][color=#000000] = %11000100                          [/color][color=#008000]' 7[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 500[/color]
[color=#800080]OUTH[/color][color=#000000] = %11110111                          [/color][color=#008000]' 8[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 500[/color]
[color=#800080]OUTH[/color][color=#000000] = %11110110                          [/color][color=#008000]' 9[/color]
 
[color=#020FC0]DO[/color]
  [color=#020FC0]HIGH[/color][color=#000000] 0                                  [/color][color=#008000]' Test potentiometer[/color]
  [color=#020FC0]PAUSE[/color][color=#000000] 100[/color]
  [color=#020FC0]RCTIME[/color][color=#000000] 0, 1, time[/color]
  [color=#020FC0]DEBUG[/color] [color=#800080]HOME[/color][color=#000000], [/color][color=#ff0000]"time = "[/color][color=#000000], [/color][color=#000080]DEC5[/color][color=#000000] time, [/color][color=#800080]CR[/color]    [color=#008000]' Display decay time[/color]
 
  [color=#020FC0]DEBUG[/color][color=#000000] ? [/color][color=#800080]IN3[/color]                             [color=#008000]' Display PB states[/color]
[color=#020FC0]LOOP[/color]



While running this program, one important thing to check is the range of decay times for the potentiometer, which will be used to change the display of the 7-Segment LED from 0-9. ·Using this range, we can calculate the decay time interval for each minute, ensuring that the minutes complete a full cycle with the potentiometer.· Each potentiometer can vary, so for the most accurate results it is very important that you check the range for your potentiometer.·
·
For example, say your potentiometer range was 1-594.· Since we want to display 10 total digits (0-9), we can divide 594 by 10 to get a decay time increment of 59.4.· This means we’ll want the number displayed on the 7-Segment LED to change whenever the decay time increases by 59:

attachment.php?attachmentid=73724

TestNumbers.bs2

This brings us to our next test program, TestNumbers.bs2.· Run this program and verify that the 7-Segment LED displays the numbers 0-9 as the user rotates the potentiometer.
[color=#008000]' TestNumbers.bs2[/color]
[color=#008000]' Test that the numbers increase from 1-9 when turning the pot.[/color]
 
[color=#008000]' {$STAMP BS2}[/color]
[color=#008000]' {$PBASIC 2.5}[/color]
 
[color=#000000]index    VAR    Nib[/color]
[color=#000000]time     VAR    Word[/color]
 
[color=#800080]OUTH[/color][color=#000000] = %00000000                [/color][color=#008000]' Clear LED display[/color]
[color=#800080]DIRH[/color][color=#000000] = %11111111                [/color][color=#008000]' Set pins 8-15 to outputs[/color]
 
[color=#020FC0]DO[/color]
  [color=#020FC0]HIGH[/color][color=#000000] 0[/color]
  [color=#020FC0]PAUSE[/color][color=#000000] 100[/color]
  [color=#020FC0]RCTIME[/color][color=#000000] 0, 1, time[/color]
 
  [color=#020FC0]LOOKDOWN[/color][color=#000000] time, <= [noparse][[/noparse] 1, 60, 119, 178, 237, 296, 355, 414,[/color]
[color=#000000]                     473, 532 ], index[/color]
 
  [color=#020FC0]LOOKUP[/color][color=#000000] index, [noparse][[/noparse] %11100111, %10000100, %11010011, %11010110,[/color]
[color=#000000]                  %10110100, %01110110, %01110111, %11000100,[/color]
[color=#000000]                  %11110111, %11110110 ], [/color][color=#800080]OUTH[/color]
[color=#020FC0]LOOP[/color]


This program uses the LOOKUP and LOOKDOWN commands to display digits on the 7-Segement LED.· First, the program takes an RCTIME measurement and stores it in the variable time.
·
HIGH 0
PAUSE 100
RCTIME 0, 1, time

The LOOKDOWN table then uses value stored in time to determine which number in the list time is smaller than.· It then loads that number’s entry number (which is 0-9 in this case) into the variable index.
·
LOOKDOWN time, <= [noparse][[/noparse] 1, 60, 119, 178, 237, 296, 355, 414,
·················· 473, 532 ], index
·
Lastly, the LOOKUP table uses the value in index to display the proper number on the 7-Segment LED.· The corresponding binary value is then loaded into the OUTH variable.
·
LOOKUP index, [noparse][[/noparse] %11100111, %10000100, %11010011, %11010110,
··············· %10110100, %01110110, %01110111, %11000100,
············· ··%11110111, %11110110 ], OUTH
·
MiniTimer.bs2

Once the circuit has been tested and the numbers 0-9 are displayed on the 7-Segment LED, there’s just a bit more to add to the code in order to complete the timer.

[color=#000000][color=#008000]' -----[noparse][[/noparse] Title ]----------------------------------------------------------------------[/color]
[color=#008000]' MiniTimer.bs2[/color]
[color=#008000]' Simple timer to time 1-9 minutes based on user input.[/color]
 
[color=#008000]' {$STAMP BS2}                               ' Stamp directive -> BS2[/color]
[color=#008000]' {$PBASIC 2.5}                              ' PBASIC directive -> v2.5[/color]
 
[color=#008000]' -----[noparse][[/noparse] Variables ]------------------------------------------------------------------[/color]
[/color] 
[color=#000000]index        VAR    Nib         [/color][color=#008000]' Variable space for LOOKUP/LOOKDOWN[/color]
[color=#000000]time         VAR    Word        [/color][color=#008000]' Decay time of potentiometer[/color]
[color=#000000]totalSecs    VAR    Word        [/color][color=#008000]' Total number of minutes entered by user[/color]
[color=#000000]duration     VAR    Word        [/color][color=#008000]' Variable space for FOR...NEXT loop[/color]
[color=#000000]frequency    VAR    Word        [/color][color=#008000]' Frequency[/color]
 
[color=#800080][color=#008000]' -----[noparse][[/noparse] Initialization ]-------------------------------------------------------------[/color]
[/color] 
[color=#800080]OUTH[/color][color=#000000] = %00000000                [/color][color=#008000]' Clear LED display[/color]
[color=#800080]DIRH[/color][color=#000000] = %11111111                [/color][color=#008000]' Set pins 8-15 to outputs[/color]
 
[color=#020FC0]FREQOUT[/color][color=#000000] 7, 100, 3500            [/color][color=#008000]' Program startup sound[/color]
[color=#020FC0]PAUSE[/color][color=#000000] 100[/color]
[color=#020FC0]FREQOUT[/color][color=#000000] 7, 100, 3500[/color]
 
[color=#020FC0][color=#008000]' -----[noparse][[/noparse] Main Routine ]---------------------------------------------------------------[/color]
[/color] 
[color=#000000][color=#020FC0]DO[/color]
[/color]  [color=#020FC0]HIGH[/color][color=#000000] 0                        [/color][color=#008000]' Get decay time measurements[/color]
  [color=#020FC0]PAUSE[/color][color=#000000] 100[/color]
  [color=#020FC0]RCTIME[/color][color=#000000] 0, 1, time[/color]
 
  [color=#020FC0]LOOKDOWN[/color][color=#000000] time, <= [noparse][[/noparse] 1, 60, 119, 178, 237, 296,     [/color][color=#008000]' Compare decay time to pre-[/color]
[color=#000000]                     355, 414, 473, 532], index      [/color][color=#008000]' determined timer values[/color]
 
  [color=#020FC0]LOOKUP[/color][color=#000000] index, [noparse][[/noparse] %11100111, %10000100, %11010011,   [/color][color=#008000]' Display 0-9 based on the[/color]
[color=#000000]                  %11010110, %10110100, %01110110,   [/color][color=#008000]' value in index[/color]
[color=#000000]                  %01110111, %11000100, %11110111,[/color]
[color=#000000]                  %11110110 ], [/color][color=#800080]OUTH[/color]
[color=#020FC0]LOOP[/color] [color=#020FC0]UNTIL[/color][color=#000000] ([/color][color=#800080]IN3[/color][color=#000000] = 1)                                 [/color][color=#008000]' Loop until button is pressed[/color]
 
[color=#000000]totalSecs = index * 60          [/color][color=#008000]' Calculate total number of 1-second increments[/color] [color=#008000]needed[/color]
                                [color=#008000]'[/color] [color=#008000]to match number of minutes selected by the[/color] [color=#008000]user.[/color]
 
[color=#020FC0]FREQOUT[/color][color=#000000] 7, 500, 3000            [/color][color=#008000]' Tone to note start of countdown[/color]
 
[color=#020FC0]FOR[/color][color=#000000] duration = totalSecs [/color][color=#020FC0]TO[/color][color=#000000] 0[/color]
  [color=#020FC0]PAUSE[/color][color=#000000] 990                                          [/color][color=#008000]' Wait 1 second[/color]
 
  [color=#020FC0]LOOKDOWN[/color][color=#000000] duration, <= [noparse][[/noparse]  0,  60, 120, 180,         [/color][color=#008000]' Compare value in duration[/color] [color=#008000]to[/color]
[color=#000000]                         240, 300, 360, 420,         [/color][color=#008000]' number of seconds[/color]
[color=#000000]                         480, 540], index[/color]
 
  [color=#020FC0]LOOKUP[/color][color=#000000] index, [noparse][[/noparse] %11100111, %10000100, %11010011,   [/color][color=#008000]' Display 0-9 based on the[/color]
[color=#000000]                  %11010110, %10110100, %01110110,   [/color][color=#008000]' value in index[/color]
[color=#000000]                  %01110111, %11000100, %11110111,[/color]
[color=#000000]                  %11110110 ], [/color][color=#800080]OUTH[/color]
[color=#020FC0]NEXT[/color]
 
[color=#020FC0]FOR[/color][color=#000000] duration = 15 [/color][color=#020FC0]TO[/color][color=#000000] 1                               [/color][color=#008000]' Play "times up" tone[/color]
  [color=#020FC0]FOR[/color][color=#000000] frequency = 3000 [/color][color=#020FC0]TO[/color][color=#000000] 3500 [/color][color=#020FC0]STEP[/color][color=#000000] 20[/color]
    [color=#020FC0]FREQOUT[/color][color=#000000] 7, duration, frequency[/color]
  [color=#020FC0]NEXT[/color]
  [color=#020FC0]FOR[/color][color=#000000] frequency = 2000 [/color][color=#020FC0]TO[/color][color=#000000] 2500 [/color][color=#020FC0]STEP[/color][color=#000000] 10[/color]
    [color=#020FC0]FREQOUT[/color][color=#000000] 7, duration, frequency[/color]
  [color=#020FC0]NEXT[/color]
[color=#020FC0]NEXT
[/color] 



The first part of the program completes the same tasks as TestNumbers.bs2.· However, instead of displaying the numbers indefinitely, the program waits for the user to press the button and then starts the timer.· Before executing the timer loop, we need to convert the number of minutes for the timer to seconds.· This allows us to use a FOR…NEXT loop and a PAUSE command to easily count down each minute.·
·
You may also notice that the program only pauses for 990 ms instead of the expected 1000 ms.· This is because when comparing the results of this timer with a stopwatch, it was found that the LOOKUP and LOOKDOWN statements take about 10 ms to execute.· So really the approximate pause time of the FOR…NEXT loop is 1000 ms.
·
Finally, additional LOOKUP and LOOKDOWN commands show the user how many minutes are left on the timer by comparing the value in the variable duration to a table of second values.
·
totalSecs = index * 60
·
FOR duration = totalSecs TO 0
· PAUSE 990
·
· LOOKDOWN duration, <= [noparse][[/noparse]· 0,· 60, 120, 180,
················· ·······240, 300, 360, 420,
························ 480, 540], index
·
· LOOKUP index, [noparse][[/noparse] %11100111, %10000100, %11010011,
················· %11010110, %10110100, %01110110,
················· %01110111, %11000100, %11110111,
················· %11110110 ], OUTH
NEXT
·
To finish, the piezospeaker emits a unique series of tones to let the user know the timer is up.· These tones can be customized to whatever you would like, so get creative!· If you need inspiration, check out Chapter 8 in What’s a Microcontroller.
·
FOR duration = 15 TO 1
· FOR frequency = 3000 TO 3500 STEP 20
··· FREQOUT 7, duration, frequency
· NEXT
· FOR frequency = 2000 TO 2500 STEP 10
··· FREQOUT 7, duration, frequency
· NEXT
NEXT
__________________________________________________ ___________________________
··
(c) 2009·by Parallax Inc - all rights reserved.··

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jessica Uelmen
Education Department
Parallax Inc.

Post Edited (Jessica Uelmen (Parallax)) : 8/25/2010 6:30:50 PM GMT

Viewing all articles
Browse latest Browse all 293

Trending Articles