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

Basic Stamp Activity

Leslie51Leslie51 Posts: 3
edited 2015-04-16 22:07 in BASIC Stamp
Greeting everyone, I am in a bit of a conundrum. Basically I have been tasked with something that’s definitely not one of my strong suites. I only know the basics of programming and now I am seriously stuck. I am hoping someone could help give me a little nudge in the right direction with writing this code. I’ve been tasked with writing a code to make microcontroller do the following using the Basic Stamp Activity Kit:



Use the following devices as specified:

Pushbutton on P2 (PB1)

An active-high LED on P5 (LED1)

Buzzer/speaker on P8 (BUZZER)

Program the controller to perform the following:
1.On start, sound the speaker at 3000 Hz for 1 second.
2.Wait until the button is pressed.
3.Once pressed:
4.Blink LED1 on for 100mS.
5.Sound the buzzer at 3000 Hz for 100 mS
6.Turn LED1 off.
7.Count up by 1 with a variable (once per press)
8.Display the count value in the DEBUG Window.
9.Wait until the button is released.
10.If the count is equal to 10:
11.Sound buzzer at 4000Hz for 1 second
12.Reset the count to 0.
13.Repeat the program from step 2.


So far this is the code that I have written:
' {$STAMP BS2}
' {$PBASIC 2.5}

counter VAR Byte

FOR counter = 1 TO 10

     DEBUG ? counter			'Display the variable of counter in DEBUG Window











PAUSE  1000				'Pause before circuit startup

FREQOUT 8, 1000, 3000                                                'Sound piezo at 3000Hz for 1 SECOND

DO

     DEBUG  ?  IN2
     
    
     IF  (IN2 = 1)  THEN
     HIGH 5
     PAUSE 50
     LOW 5
     PAUSE 50

     ELSE
       PAUSE 100

     ENDIF

FREQOUT 8, 100, 3000			'Sound piezo at 3000Hz for 100mS

FREQOUT 8, 1000, 4000			'Sound piezo at 4000Hz for 1 sec

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2015-04-09 07:57
    First step is to rewrite the steps you've been given using indenting to reflect which steps are dependent on other steps. For example, step #3 controls steps #4 through #12 and those should be indented under #3. Similarly, steps #2 through #12 are part of the loop described in step #13 (there's a point between #1 and #2 that goes with #13). Once you have that done, you should be able to translate the indenting into loops using DO / LOOP statements. Copy your steps as comments into your code as you've started doing, but keep the step numbers in the comments. Translate the comments one by one into code as you've started doing. Come back to the forum with specific questions as they come up. As usual, "What's a Microcontroller?" and the "Basic Stamp Syntax and Reference Manual" have lots of examples and descriptions of the sorts of things you need to do. The Manual is downloadable for free from Parallax's website ... look in the webstore for the Manual and you'll find a link for downloading.
  • ercoerco Posts: 20,257
    edited 2015-04-09 08:04
    Sounds like a great school assignment. Per Mike, read the material and enjoy learning the right way. We'll nudge when you show you're trying. :)
  • GenetixGenetix Posts: 1,754
    edited 2015-04-09 11:21
    The first part of your program is perfect but the rest is a mess.

    This should help you with the button since the BASIC Stamp Manual doesn't show this example for the DO...LOOP
    DO : LOOP UNTIL (Put the condition that you are waiting for in here)
    

    Are you sure the buzzer is beeped for a count of 10 AFTER the button is released?

    https://www.parallax.com/sites/default/files/downloads/27218-Web-BASICStampManual-v2.2.pdf
    DO...LOOP - Page 179 of PDF
    IF...THEN - Page 235 of PDF

    I highly suggest that you use Constants and Directives as shown starting on page 160.
    It's also very good practice to comment your programs such as the one on page 299.
    https://www.parallax.com/sites/default/files/downloads/28123-Whats-a-Micro-v3.0.pdf
  • Leslie51Leslie51 Posts: 3
    edited 2015-04-09 11:24
    I can appreciate and respect your honesty, I went back and read through some things and this is what I have now:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    PAUSE 1000				'PAUSE 1 SECOND BEFORE CIRCUIT STARTUP
    
    FREQOUT 8, 1000, 3000                   'SOUND PIEZO AT 3000Hz FOR 1 SECOND
    
    DEBUG "DEPRESS PUSHBUTTON",CR		'INSTRUCTS OPERATOR TO DEPRESS PUSHBUTTON
    
    
    counter VAR Nib				'WILL COUNT NUMBERS FROM 0-15 
    
    FOR counter = 1 TO 10
    
    
    COUNTER = 0				'STARTS COUNTER AT ZERO
    
    DO					'BEGINS MAIN LOOP
      
       DO					'BEGIN NESTED LOOP
       LOOP UNTIL IN2 = 1			'BREAK OUT OF MAIN LOOP WHEN I/O PIN 2 (PB1) IS HIGH
      					
        IF (IN2 = 1)  THEN			'ONCE PUSHBUTTON IS DEPRESSED, BLINK LED1 FOR 100 MILLISECONDS
        HIGH 5				'ILLUMINATES LED ON P5 (LED1)
        PAUSE 50				
        LOW 5				'DE-ENERGIZES LED ON P5 (LED1)
        PAUSE 50
       FREQOUT 8, 100, 3000			'SOUND PIEZO AT 3000HZ FOR 100 MILLISECONDS 
    
       LOOP UNTIL (IN2 = 0)
    
         LOW 5				'TURNS LED1 OFF
    
    COUNTER = COUNTER+1			'ADDS 1 TO COUNTER(?)
    
    DEBUG ? counter				'DISPLAY THE VARIABLE OF COUNTER IN DEBUG WINDOW
    
        DO
         IF IN2 = 1) THEN, 
          DEBUG "RELEASE PUSHBUTTON",CR	'INSTRUCTS OPERATOR TO RELEASE PUSHBUTTON IF DEPRESSED	
         ELSEIF (TIMECOUNTER = 10) THEN	'STOPS LOOP WHEN COUNTER REACHES 10 
         
    
    NEXT
    
    
    FREQOUT 8, 1000, 4000			'SOUND PIEZO AT 4000HZ FOR 1 SEC
        LOOP
       ENDIF
    
    
  • Leslie51Leslie51 Posts: 3
    edited 2015-04-09 11:26
    Talk about brute honesty...I like it! I have just implemented some changes to the DO...LOOP. can you tell me what you think?
  • GenetixGenetix Posts: 1,754
    edited 2015-04-09 14:12
    Better!
    The 1 second PAUSE gives the computer time to prepare for BS2 serial communication.
    Variables are usually declared first.
    When would Counter change if you used FOR...NEXT?
    What's wrong with DEBUG paced BEFORE the main DO...LOOP?
    You make Counter = 0 but FOR...NEXT started it with 1.
    Which LOOP goes with which DO and what is it there for?
    What does Step 4 tell you to do?
    There is a LOOP UNTIL (IN2 = 0) but what Step is this?
    Are you or is FOR...NEXT in control of Counter?

    Might this DO...LOOP after DEBUG Counter be a problem?
    Where should Step 9 be?
    What variable is TimeCounter?
    How does FOR...NEXT work again and what happens to anything after NEXT?
    What are LOOP and ENDIF connected to and are they in the correct order?
  • Leslie51Leslie51 Posts: 3
    edited 2015-04-10 21:25
    Thanks so much Genetix! You've helped me out tremendously and I am actually learning this stuff...well at least I hope so. Here is my latest revision
    
    ' {$STAMP BS2}
    
    ' {$PBASIC 2.5}
    
    counter VAR Byte       'DECLARES VARIABLE TO COUNT (0-255)
    
    PAUSE 1000                                'PAUSE 1 SECOND BEFORE CIRCUIT STARTUP
    
    FREQOUT 8, 1000, 3000                     'SOUND PIEZO AT 3000Hz FOR 1 SECOND
    
    PAUSE 500                                 'PAUSE 1/2 SECOND
    
    MAIN:
    
    DO                                        'STARTS MAIN LOOP
    
    COUNTER = 0                               'STARTS COUNTER AT ZERO
    
         DO                                   'STARTS NESTED LOOP
    
           LOOP UNTIL IN2 = 1                 'CONTINUE MAIN LOOP UNTIL PB1; HIGH I/O PIN 2
    
             IF (IN2 = 1) THEN                'ONCE P/B IS DEPRESSED, BLINK LED1 FOR 100 MILLISECONDS
               COUNTER = COUNTER +1           'ADDS 1 TO COUNTER VARIABLE
                HIGH 5                        'ILLUMINATES LED ON P5 (LED1)
                PAUSE 100
                LOW 5                         'DE-ENERGIZES LED ON P5 (LED1)
               PAUSE 100
              FREQOUT 8, 100, 3000            'SOUND PIEZO AT 3000HZ FOR 100 MILLISECONDS
              ELSE
              THEN GOTO MAIN
              LOOP UNTIL IN2 = 0              'LOOPS NESTED LOOP UNTIL P/B RELEASED
    
    DO
    IF COUNTER = 10 THEN                      'WHEN COUNTER REACHES 10
    FREQOUT 8, 1000, 4000                     'SOUND PIEZO AT 4000HZ FOR 1 SEC
    ENDIF
    
    DEBUG ? counter                           'DISPLAY THE VARIABLE OF COUNTER IN DEBUG WINDOW
    
    LOOP                                      'RETURNS TO MAIN LOOP AWAITING HIGH ON PB1
    
    
  • GenetixGenetix Posts: 1,754
    edited 2015-04-13 00:38
    Leslie, I cleaned up the code you wrote to make it easier to read. Hopefully some of the mistakes are obvious now.
    ' {$STAMP BS2}
    
    ' {$PBASIC 2.5}
    
    ' Variable Declaration
      counter VAR Byte                          'DECLARES VARIABLE TO COUNT (0-255)
    
    ' Initialization
      PAUSE 1000                                'PAUSE 1 SECOND BEFORE CIRCUIT STARTUP
    
      FREQOUT 8, 1000, 3000                     'SOUND PIEZO AT 3000Hz FOR 1 SECOND (Step 1)
    
      PAUSE 500                                 'PAUSE 1/2 SECOND
    
    ' Main Loop
    
      MAIN:                                     '     [Label - Main Subroutine]
    
      DO                                        'STARTS MAIN LOOP (Loops Forever)     [Start of Main DO...LOOP]
    
        COUNTER = 0                             'STARTS COUNTER AT ZERO (Step 12)
    
        DO                                      'STARTS NESTED LOOP     [Begin Inner Loop #1]
        LOOP UNTIL (IN2 = 1)                    'CONTINUE MAIN LOOP UNTIL PB1; HIGH I/O PIN 2 (Step 2)     [End Inner Loop #1]
    
        IF (IN2 = 1) THEN                       'ONCE P/B IS DEPRESSED, BLINK LED1 FOR 100 MILLISECONDS (Step 3)     [Start of IF #1]
          COUNTER = COUNTER +1                  'ADDS 1 TO COUNTER VARIABLE (Step 7)
          HIGH 5                                'ILLUMINATES LED ON P5 (LED1) [Step 4]
          PAUSE 100                             ' (Wait 1/10 second)
          LOW 5                                 'DE-ENERGIZES LED ON P5 (LED1) [Step 6]
          PAUSE 100                             ' (Wait 1/10 second)
          FREQOUT 8, 100, 3000                  'SOUND PIEZO AT 3000HZ FOR 100 MILLISECONDS (Step 5)
        ELSE
          THEN GOTO MAIN
                                                '      [End of IF #1]
    
        LOOP UNTIL (IN2 = 0)                    'LOOPS NESTED LOOP UNTIL P/B RELEASED (Step 9)     [End of Inner Loop #2]
    
        DO                                      '      [Start of Inner Loop #2]
    
        IF COUNTER = 10 THEN                    'WHEN COUNTER REACHES 10 (Step 10)     [Start of IF #2]
          FREQOUT 8, 1000, 4000                 'SOUND PIEZO AT 4000HZ FOR 1 SEC (Step 11)
        ENDIF                                   '      [End of IF #2]
    
        DEBUG ? counter                         'DISPLAY THE VARIABLE OF COUNTER IN DEBUG WINDOW (Step 8)
    
    LOOP                                      'RETURNS TO MAIN LOOP AWAITING HIGH ON PB1 (Step 13)     [End of Main DO...LOOP]
    




    Notice the Outer Main DO...LOOP?
    This loop will repeat forever unless you so your GOTO MAIN won't function. Besides it's bad practice to do that.

    Counter = 0 will run everytime the loop repeats since you made it the first command. How does that affect the value of Counter?

    What do you notice about Inner Loop #1 and the IF statement just below it?

    That long IF...THEN...ELSE is missing an ENDIF at the end.

    Many of your steps are out of order.
    What does Step 4 say to do?

    Inner Loop #2 is backwards.

    Look again at what you program is doing and does it match what you were told to do?
    If you don't understand a command of how a segment of code works then ask.

    Good Indenting and Commenting make a program easier to read and understand.
  • ercoerco Posts: 20,257
    edited 2015-04-14 07:02
    @Genetix: Wonderful tutoring method, cleaning up the code for readability. I will strive to follow your example.
  • GenetixGenetix Posts: 1,754
    edited 2015-04-16 22:07
    Erco, it's one thing to tell someone something, but another for them to see it for themselves.
    I just needed to filter out all the noise.

    Actually, you should credit Mr. Green since I learned the basic technique from him.
Sign In or Register to comment.