Creating a Program with BASIC Stamp (2.5.3) and LEDs
jellybean1865
Posts: 7
I have to design a program that will turn on an LED with a push button and cause it to blink 10 times at a rate of 1/s, with the LED on for 0.5 s and off for 0.5 s during each cycle. If the button is pressed again before the LED blinks 10 times, the LED should automatically turn itself off. I am supposed to use IF THEN statements. I am not sure how to get the LED to blink ten times after it is pushed, or how to make it stop if it is pushed again before the ten blinks have happened. Any help is appreciated!!
My code so far is: (and I know this isn't right)
' What's a Microcontroller - PushbuttonControlblink10.bs2
' Check pushbutton state 10 times per minute, blink LED 10 times when button pushed, and stop blinking if pushed again before 10 blinks
' {$STAMP BS2}
' {$PBASIC 2.5}
DO
DEBUG ? IN3
IF (IN3 = 1)THEN
HIGH 14
PAUSE 50
LOW 14
PAUSE 50
ELSE
PAUSE 100
ENDIF
LOOP
My code so far is: (and I know this isn't right)
' What's a Microcontroller - PushbuttonControlblink10.bs2
' Check pushbutton state 10 times per minute, blink LED 10 times when button pushed, and stop blinking if pushed again before 10 blinks
' {$STAMP BS2}
' {$PBASIC 2.5}
DO
DEBUG ? IN3
IF (IN3 = 1)THEN
HIGH 14
PAUSE 50
LOW 14
PAUSE 50
ELSE
PAUSE 100
ENDIF
LOOP
Comments
I'm sure this was covered in your class.
The other thing is, the pause command is milliseconds, so you'd need pause 500 to get half a second delay, not 50 as you have.
And, you must remember that anything you have inside the loop will execute everytime through the loop, so if you have some sort of setup that needs done, or conditions that must be met before the instructions in the loop take over, then those things should be done outside the loop, otherwise you'll get incorrect results.
hth.
' {$PBASIC 2.5}
counter VAR Byte
FOR counter = 1 TO 10
DEBUG ? counter
HIGH 14
PAUSE 500
LOW 14
PAUSE 500
NEXT
DEBUG "All done!"
END
ok got this part. still not sure how to get it to stop if button is pressed before 10 blinks. thanks for all the help!
Hint: You only need three more lines of code and you'll be done. Read about IF...THEN in the Basic Stamp Editor help files.
' {$PBASIC 2.5}
counter VAR Byte
IF (IN3 = 1) THEN
{IF (IN3 = 0) THEN
FOR counter = 1 TO 10
DEBUG ? counter
HIGH 14
PAUSE 500
LOW 14
PAUSE 500
ELSE (IN3 = 1) THEN
LOW 14
PAUSE 500
}
ELSE (IN3 = 0)
PAUSE 100
ENDIF
LOOP
DEBUG "All done!"
END
still not sure but i tweaked some
What you are doing now results in sloppy code which probably does not even compile and have no chance to do what you want to do.
And even the simplest code should include some comments.
For example if you added " .5s LED on" it would be obvious from start that PAUSE 50 was wrong.
And my favorite "complaint" - if the variable has only two values possible - 0 and 1 - your code does not have to check for both - in pseudo code:
if variable = 1 then
do something...
(esle) do something else... variable is zero
end
A final hint - to monitor the pushbutton you need to replace PAUSE 500 with code reading the button state.
Condition = Button is pressed
Address = END
Put this in your program from post #5. Put it inside the blinking loop at the end. This will work but it has a bit of a problem - response time.
The program cannot check the button while it is doing the PAUSE commands, so the button must be down while the program runs the IF...THEN command.
In other words, if the button is pressed quickly it may not register, to be sure the program ends it may have to be held down for a full second. You could put two of these commands in, one after each PAUSE and reduce this to 1/2 second, but there are better ways to fix this problem.
Hint: Replace the long PAUSE commands with loops using much shorter PAUSE commands while checking the state of the button.
Once you have that it is MUCH easier to write code.
1. Read byte 4. - yours would be - input button.
2. Add the new 8-bit value to it.
3. If the sum is less than the original value of byte 4, go to step 4; otherwise, quit.
4. Add one to byte 3. If the sum is zero, go to step 5; otherwise, quit.
5. Add one to byte 2. If the sum is zero, go to step 6; otherwise, quit.
PS Is this a school project??
Actually, you don't have to go through all that just to read a pin. You can just use IN to read the status of any of the 16 i/o pins directly. They are set to be inputs by default on start up. So if IN3 = 1 it means the button is pressed (the exercises in that section of the book have the pin wired to Vdd through a 220 ohm resistor with a 10k pulldown to ground). IF IN3 = 1 THEN END will cause the program to stop. As I stated above, the problem is that the program is spending most of its time PAUSEd.
Well, looks like we have managed to chase the OP off.
My point exactly, if (!) this is a school assignment, than (!) I would like to hear from the teacher.
I like to know if this is current state of the art teaching method - write incomplete "to do " statement, read "What is a microprocessor?" and than start coding. And when in trouble - post on internet.
The OP had no clue how to count events, read input and implement if... than, comments etc.
Reminds me of How about Bob? - baby steps, baby steps.
Correction: "What About Bob", One of my favorites " I'm Sailing " !!!!!!