Basic Stamp Activity
Leslie51
Posts: 3
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:
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
This should help you with the button since the BASIC Stamp Manual doesn't show this example for the DO...LOOP
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
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?
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.
I just needed to filter out all the noise.
Actually, you should credit Mr. Green since I learned the basic technique from him.