Shop OBEX P1 Docs P2 Docs Learn Events
Basic Stamp Project — Parallax Forums

Basic Stamp Project

cwallacecwallace Posts: 6
edited 2015-03-27 20:07 in BASIC Stamp
I'm new to programming and micro controllers and I'm trying to teach myself by using the basic stamp kit (90005 - USB). I saw a project idea online that I would like to try, but I'm having trouble writing the code. The circuit set up I don't think I will have any problems with. Here's what I'm trying to do, any help with the code will be much appreciated.

Parts:
1. Pushbutton
2. (2) LEDs
3. Buzzer

Set up:
1. Pushbutton on P2
2. One LED on P5
3. One LED on P6
4. Buzzer on P8
Both buzzers I want to be active highs.

This is what I'm trying to do:
1. Upon power up, the buzzer should sound off at 3kHz for 1 sec.
2. Then when I depress the button, it will count up by one. (For each press of the button).
3. Upon release of the button, display the count in the debug window.
4. If the count is one, blink LED 1 on and off.
5. If the count is two, blink LED 2 on and off.
6. If the count is three, blink both LEDs on and off at the same time.
7. If the count is four, sound the buzzer at 3kHz for 1 sec, reset the count back to zero.
8. Wait for the button to be released.
9. Repeat from step two.

I want to bring it in to work to impress my co-workers. Thanks in advance.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2015-03-26 19:31
    First of all, you need two texts: "What's a Microcontroller?" (https://www.parallax.com/product/28123) and the "Basic Stamp Syntax and Reference Manual" (https://www.parallax.com/product/27218). You can either buy a paper copy or download a PDF file for free. The first has lots of examples of what you're trying to do (control a buzzer or LED, check a pushbutton, etc.) The second describes how to use the Stamp Editor and how to use each of the statements among other things. Check to make sure your buzzer is designed to work at 5V and the amount of current it uses is less than 20mA. If it uses more current than that, you'll need a transistor to turn the buzzer on and off. This is described in Nuts and Volts Column #6 (https://www.parallax.com/downloads/nuts-and-volts-basic-stamps-volume-1). For the LEDs, you'll need a current limiting resistor, typically 330 Ohms for a standard red LED. For the pushbutton, you'll need a pull-up or pull-down resistor. 10K Ohms is commonly used for this. Both texts show how to connect these.

    First get each element of your project to work individually (buzzer, LEDs, pushbutton) using the examples in the texts. Use the BUTTON statement to test the pushbutton ... that takes care of debouncing for you. For the LEDs, have them turn on when the pin is high ... see the description in the first text. Typically, you'd use the following statements: HIGH <pin> then PAUSE <delay> then LOW <pin>. The delay (in ms) would be 1000 for a buzzer or something smaller for an LED blink.
  • ercoerco Posts: 20,256
    edited 2015-03-26 21:36
    cwallace, that's a great project and goal for you. You'll learn much valuable info as you progress in your project. Many people here could (and wouldn't mind) write the code for you in a minute or two. But that would rob you of the joy and discovery of learning. Per Mike, start with WAM, learn the basics, start writing your code one bit at a time. Post back here with specific questions, we're happy to nudge you on your journey.
  • cwallacecwallace Posts: 6
    edited 2015-03-27 08:32
    Thanks Mike and Erco for your quick response and inputs. I know this is no wear near completion, but can you guys look at my initial programming attempt and see if I'm on the right track. I still need to look up how to program the tone. Thanks in advance.
    'What's a Microcontroller - Programming Challenge.bs2
    'Sound the alarm at start up. Count pushbutton state 4 times and blink specific LEDs when pressed. Sound alarm upon completion.
    
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    PAUSE 1000                                   ' Wait 1 sec before 1st message.
    
    HIGH 8                                       ' Sound the alrm to indicate start up.
    PAUSE 1000
    LOW 8
    
    timeCount VAR Word                           ' Declare variable to store count.
    
    DO                                           ' Begin main loop.
    
      DO                                         ' Nested loop repeats...
      LOOP UNTIL IN2 = 1                         ' until pushbutton press.
    
    IF IN2 = 1, THEN
    
    timeCounter = 0                              ' Set timeCounter to zero.
    
    DO                                           ' Nested loop, count time...
    
      PAUSE 1
      timeCounter = timeCounter +1
    
    IF timecounter = 1, THEN
    
      HIGH 5                                     ' LED1 red, blink on and off.
      PAUSE 500
      LOW 5
    
    LOOP UNTIL IN2 = 0                           ' Loop until pushbutton is released.
    
    DEBUG "Current count is"                     ' Display current count.
    
      DO                                         ' Nested loop, count...
    
      PAUSE 1000
    
    LOOP                                         ' Back to "Begin main loop".
    
    IF IN2 = 1, timecounter = 2, THEN
    
      HIGH 6                                     ' LED2 green, blink on and off.
      PAUSE 500
      LOW 6
    
    DO                                           ' Nested loop, count time...
    
      PAUSE 1
      timeCounter = timeCounter +1
    
    LOOP UNTIL IN2 = 0                           ' Loop until pushbutton is released.
    
    DEBUG "Current count is"                     ' Display current count.
    
      DO                                         ' Nested loop, count...
    
      PAUSE 1000
    
    LOOP                                         ' Back to "Begin main loop".
    
    IF IN2 = 1, timecounter = 3, THEN
    
      HIGH 5
      HIGH 6                                     ' Both LEDs, blink on and off.
      PAUSE 500
      LOW 5
      LOW 6
    
    DO                                           ' Nested loop, count time...
    
      PAUSE 1
      timeCounter = timeCounter +1
    
    LOOP UNTIL IN2 = 0                           ' Loop until pushbutton is released.
    
    DEBUG "Current count is"                     ' Display current count.
    
      DO                                         ' Nested loop, count...
    
      PAUSE 1000
    
    IF IN2 = 1, timecounter = 4, THEN
    
      HIGH 8                                     ' Sound the alarm to end sequence.
      PAUSE 500
      LOW 8
    
    LOOP                                         ' Back to "Begin main loop".
    
  • PublisonPublison Posts: 12,366
    edited 2015-03-27 08:40
    Welcome to the forum!

    Code will be better displayed using these instructions:

    attachment.php?attachmentid=78421&d=1297987572

    It will preserve the indentation, making it easier to read.
  • cwallacecwallace Posts: 6
    edited 2015-03-27 10:28
    Thanks. That was very helpful and it does look better organized and easier to read.
  • cwallacecwallace Posts: 6
    edited 2015-03-27 10:30
    Thanks Mike for your quick response and inputs. I know this is no wear near completion, but can you guys look at my initial programming attempt and see if I'm on the right track. I still need to look up how to program the tone. Thanks in advance.

    'What's a Microcontroller - Programming Challenge.bs2
    'Sound the alarm at start up. Count pushbutton state 4 times and blink specific LEDs when pressed. Sound alarm upon completion.
    
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    PAUSE 1000                                   ' Wait 1 sec before 1st message.
    
    HIGH 8                                       ' Sound the alrm to indicate start up.
    PAUSE 1000
    LOW 8
    
    timeCount VAR Word                           ' Declare variable to store count.
    
    DO                                           ' Begin main loop.
    
      DO                                         ' Nested loop repeats...
      LOOP UNTIL IN2 = 1                         ' until pushbutton press.
    
    IF IN2 = 1, THEN
    
    timeCounter = 0                              ' Set timeCounter to zero.
    
    DO                                           ' Nested loop, count time...
    
      PAUSE 1
      timeCounter = timeCounter +1
    
    IF timecounter = 1, THEN
    
      HIGH 5                                     ' LED1 red, blink on and off.
      PAUSE 500
      LOW 5
    
    LOOP UNTIL IN2 = 0                           ' Loop until pushbutton is released.
    
    DEBUG "Current count is"                     ' Display current count.
    
      DO                                         ' Nested loop, count...
    
      PAUSE 1000
    
    LOOP                                         ' Back to "Begin main loop".
    
    IF IN2 = 1, timecounter = 2, THEN
    
      HIGH 6                                     ' LED2 green, blink on and off.
      PAUSE 500
      LOW 6
    
    DO                                           ' Nested loop, count time...
    
      PAUSE 1
      timeCounter = timeCounter +1
    
    LOOP UNTIL IN2 = 0                           ' Loop until pushbutton is released.
    
    DEBUG "Current count is"                     ' Display current count.
    
      DO                                         ' Nested loop, count...
    
      PAUSE 1000
    
    LOOP                                         ' Back to "Begin main loop".
    
    IF IN2 = 1, timecounter = 3, THEN
    
      HIGH 5
      HIGH 6                                     ' Both LEDs, blink on and off.
      PAUSE 500
      LOW 5
      LOW 6
    
    DO                                           ' Nested loop, count time...
    
      PAUSE 1
      timeCounter = timeCounter +1
    
    LOOP UNTIL IN2 = 0                           ' Loop until pushbutton is released.
    
    DEBUG "Current count is"                     ' Display current count.
    
      DO                                         ' Nested loop, count...
    
      PAUSE 1000
    
    IF IN2 = 1, timecounter = 4, THEN
    
      HIGH 8                                     ' Sound the alarm to end sequence.
      PAUSE 500
      LOW 8
    
    LOOP                                         ' Back to "Begin main loop".
    
  • GenetixGenetix Posts: 1,749
    edited 2015-03-27 11:10
    The BASIC Stamp Activity Kit has a Piezo Speaker, not a buzzer, so you need to use FREQOUT not HIGH or LOW.
    Refer to Chapter 8, Activity 1 of What's a Microcontroller? (WAM - Page 246)
    https://www.parallax.com/sites/default/files/downloads/28123-Whats-a-Micro-v3.0.pdf
    Page 199 (PDF Page 203) of the BASIC Stamp Manual explains FREQOUT in depth.
    https://www.parallax.com/sites/default/files/downloads/27218-Web-BASICStampManual-v2.2.pdf

    I would suggest you refer to Chapter 5, Activity 4 on Declaring Constants and Pin Directives (WAM - Page 160, code of page 162)
    This is also a good reference on good programming.
    http://www.parallax.com/go/PBASICHelp/Content/LanguageTopics/Reference/ElementsStyle.htm
  • cwallacecwallace Posts: 6
    edited 2015-03-27 13:01
    Thanks Genetix. I've made some changes to the programming. Here's what I currently have:
    'What's a Microcontroller - Programming Challenge.bs2
    'Sound the alarm at start up. Count pushbutton state 4 times and blink specific LEDs when pressed. Sound alarm upon completion.
    
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    PAUSE 1000                                     ' Wait 1 sec before 1st message.
    
      FREQOUT 8, 1000, 3000                        ' Sound the alarm to indicate start up.
      PAUSE 1000
    
    counter VAR Word                               ' Declare variable to store count.
    
    DO                                             ' Begin main loop.
        counter = 0                                      ' Set counter to zero.
       DO                                                ' Nested loop repeats...
        LOOP UNTIL IN2 = 1                               ' Until pushbutton press.
    
       IF (IN2 = 1) THEN
        DO                                               ' Nested loop, count...
        PAUSE 1
        counter = counter +1
    
       IF (counter = 1) THEN
        HIGH 5                                           ' LED1 red, blink on and off.
        PAUSE 500
        LOW 5
       ELSE
        LOOP UNTIL IN2 = 0                               ' Loop until pushbutton is released.
        DEBUG "Current count is", DEBUG ? DEC counter    ' Display current count.
    
      DO                                                 ' Nested loop, count...
       PAUSE 1000
    
      LOOP                                               ' Back to "Begin main loop".
    
      IF (counter = 2) THEN
        HIGH 6                                           ' LED2 green, blink on and off.
        PAUSE 500
        LOW 6
       ELSE
        LOOP UNTIL IN2 = 0                               ' Loop until pushbutton is released.
        DEBUG "Current count is", DEBUG ?  DEC counter   ' Display current count.
    
      DO                                                 ' Nested loop, count...
       PAUSE 1000
    
      LOOP                                               ' Back to "Begin main loop".
    
      IF (counter = 3) THEN
        HIGH 5
        HIGH 6                                           ' Both LEDs blink on and off.
        PAUSE 500
        LOW 5
        LOW 6
       ELSE
        LOOP UNTIL IN2 = 0                               ' Loop until pushbutton is released.
        DEBUG "Current count is", DEBUG ? DEC counter    ' Display current count.
    
      DO                                                 ' Nested loop, count...
       PAUSE 1000
    
      LOOP                                               ' Back to "Begin main loop".
    
      IF (counter = 4) THEN
       FREQOUT 8, 1000, 3000                             ' Sound the alarm to indicate start up.
       ELSE
        LOOP UNTIL IN2 = 0                               ' Loop until pushbutton is released.
        DEBUG "Current count is", DEBUG ? DEC counter    ' Display current count.
    
    LOOP                                                 ' Back to "Begin main loop".
    
    
    
  • GenetixGenetix Posts: 1,749
    edited 2015-03-27 14:40
    Look up DO...LOOP in the BASIC Stamp Manual because you don't seem to understand how to use. Anything inside a DO...LOOP will repeat forever and ever.
    If you look up PAUSE, you will see that uses increments of a millisecond so 1 is too short while 1000 may be longer than you think.
    Also look up IF...THEN because you have more than 1 statement following the THEN so there should be an ENDIF at the end of the block.

    Might I suggest looking up SELECT...CASE since that fits your situation with Counter better than IF...THEN.

    I think this code in the old Applied Sensors text may be what you are trying to do with the pushbuttons.
    Chapter 2 from page 28 (page 36 of the PDF) through page 37 (page 45 of the PDF) has code for detecting short and long button presses as well as making sounds with the Piezospeaker.
    http://www.digikey.com/Web%20Export/Supplier%20Content/Parallax_149/PDF/Parallax_AppliedSensorsGuide.pdf?redirected=1
  • cwallacecwallace Posts: 6
    edited 2015-03-27 20:07
    My hat goes off to you guys who have a solid grasps of programming. It always looks easy when you see some else doing it, but I want to conqueror this. Here's my latest changes:
    'What's a Microcontroller - Programming Challenge.bs2
    'Sound the alarm at start up. Count pushbutton state 4 times and blink specific LEDs when pressed. Sound alarm upon completion.
    
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    counter VAR Byte                                       ' Declare variable to store count.
    PB1 VAR Byte
    PB1 = IN2
    
    
    PAUSE 100                                              ' Wait 1/10 sec before start.
    
      FREQOUT 8, 1000, 3000                                ' Sound a 3kHz alarm for 1 sec to indicate start up.
      PAUSE 100
    
    DO                                                     ' Begin main loop.
    counter = 0                                            ' Set counter to zero.
      DO                                                   ' Nested loop repeats...
       LOOP UNTIL PB1 = 1                                  ' Until pushbutton depress.
       IF (PB1 = 1) THEN
        DO                                                 ' Nested loop, counts...
        PAUSE 100
        counter = counter +1
    
         IF (counter = 1) THEN                             ' LED1 red, blinks on and off.
          HIGH 5
          PAUSE 50
          LOW 5
         ELSEIF (counter = 2) THEN                         ' LED2 green, blinks on and off.
          HIGH 6
          PAUSE 50
          LOW 6
         ELSEIF (counter = 3) THEN                         ' Both LEDs blink on and off.
          HIGH 5
          HIGH 6
          PAUSE 50
          LOW 5
          LOW 6
         ELSEIF (counter = 4) THEN                         ' Sound the alarm to indicate end of 4 count sequence.
          FREQOUT 8, 1000, 3000
         ELSE
          LOOP UNTIL (PB1 = 0)                             ' Loop until pushbutton is released.
         ENDIF
          DEBUG "Current count is", DEBUG ? DEC counter    ' Display current count.
      LOOP                                                 ' Back to nested loop repeats...
    
    LOOP                                                   ' Back to main loop.
    
    
    


    Parts:
    1. Pushbutton
    2. (2) LEDs
    3. Buzzer

    Set up:
    1. Pushbutton ON P2
    2. One LED ON P5
    3. One LED ON P6
    4. Buzzer ON P8
    Both LEDs I want TO be active highs.

    This is what I'm trying to do:
    1. Upon power up, the buzzer should sound off at 3kHz FOR 1 sec.
    2. THEN when I depress the BUTTON, it will COUNT up by one. (FOR each press of the BUTTON).
    3. Upon release of the BUTTON, display the COUNT in the DEBUG window.
    4. IF the COUNT is one, blink LED 1 ON AND off.
    5. IF the COUNT is two, blink LED 2 ON AND off.
    6. IF the COUNT is three, blink both LEDs ON AND off at the same time.
    7. IF the COUNT is four, sound the buzzer at 3kHz FOR 1 sec, reset the COUNT back TO zero.
    8. WAIT FOR the BUTTON TO be released.
    9. Repeat from STEP two.
Sign In or Register to comment.