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