Basic Stamp Project
cwallace
Posts: 6
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.
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
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.
Code will be better displayed using these instructions:
It will preserve the indentation, making it easier to read.
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
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
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.