Shop OBEX P1 Docs P2 Docs Learn Events
What other code could I use other than button? — Parallax Forums

What other code could I use other than button?

ERMERM Posts: 34
edited 2005-07-06 20:52 in BASIC Stamp
Hey guys,

I'm trying to write a program where I can change a set pattern of flashing led's to another set pattern by just the push of a button. I've learned that the button command will only work when the button is pressed at the end of the flash pattern cycle if the code is at the end. This does not help me if the flash pattern is long and you have to wait till the end of the flash pattern to change to a new pattern. I want to create a code where I can press the button to forward to the next pattern at any time during the pattern cycle. I also wanted to be able to tell it that if the user holds down the button for 3 seconds, then execute a different command like STOP. I know its possible, I just haven't figured it out yet. Can anyone help with this? Here is a sample of the code I wrote:

'{$STAMP BS2}

Reps VAR NIB
BtnWk VAR BYTE


variablespeedsingle:

for Reps = 1 to 4
high 1
pause 100
low 1
pause 100
high 2
pause 100
low 2
pause 100
NEXT

for Reps = 1 to 4
high 1
pause 75
low 1
pause 75
high 2
pause 75
low 2
pause 75
NEXT

for reps = 1 to 4
high 1
pause 50
low 1
pause 50
high 2
pause 50
low 2
pause 50
NEXT

for Reps = 1 to 4
high 1
pause 75
low 1
pause 75
high 2
pause 75
low 2
pause 75
NEXT

BUTTON 0,1,255,250,BtnWk,0,variablespeedsingle

goto doubleflash

I know there is a better way to write this, but I'm working with one problem at a time. Right now I'm looking to be able to change the flash pattern at any point in the sequence. If you can help, I would really appreciate it. Thanks in advance.

Tony

Comments

  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2005-07-06 14:22
    I realize you have probably thought of this but you could put the BUTTON check in each of the for loops. It might slow the program down a bit but it should work
  • Don BuczynskiDon Buczynski Posts: 31
    edited 2005-07-06 20:52
    You could use a program loop instead of the BS2 button command. The general idea is:

    1. Read the button state using an INPUT command. Each time the button changes from 0 to 1·increment a variable. See this link for some ideas.· http://www.emesystems.com/BS2fsm.htm

    2. Branch to a rep/delay routine·based upon the current variable value. Each rep/delay routine performs one step of the repitition sequence, increments·its private counter·and then exits.·Each·private counter·is used to know which step or "state" to set in·each routine.·This way, you don't have to wait for the entire sequence to complete before you test the button again. Read about "State Machines" on the above link for other ideas.

    3. Add a counter·to your main loop that increments whenever the button·input is 1 and resets to·zero when the button input is 0. A test for a·value that represents 3 seconds worth of loops can be used as your stop indication.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don Buczynski

    http://www.buczynski.com
Sign In or Register to comment.