Shop OBEX P1 Docs P2 Docs Learn Events
BUTTON Command — Parallax Forums

BUTTON Command

PRPROG01PRPROG01 Posts: 28
edited 2014-11-26 09:00 in BASIC Stamp
Hi.
I have a button on pin 0 that I need to have the following behavior: When press it must generate a "ON" status (set a variable = 1) and when release an "OFF" status (set the same variable to 0). No repeats when press. I am exploring the following code. So far when I press the button it show the ON status with no repeats. Good. But when release It display the "OFF" message infinitely. Do I have to used the BUTTON command? It was very difficult to get to this stage , the BUTTON command is not that easy to understand (...at least for me)

btnWrk VAR Byte
Main:
BUTTON 0, 1, 255, 0, btnWrk, 0, Off
DEBUG "On", CR
Off:
DEBUG "Off" , CR
GOTO Main

Thanks,
PRPROG

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2014-11-26 08:03
    Your program is doing exactly what you've written it to do. It sounds like what you want is a program with two states. Initially, you want the program to just sit there until the button is pressed, then do something (like display a message) and go into a 2nd state. In the 2nd state, the program just sits there until the button is released, then does something. Each state is implemented with a loop, so you need a program with two loops like this:

    Initial:
    If <button is pressed> THEN
    DEBUG "On",CR
    GOTO 2ndState
    ENDIF
    GOTO Initial
    2ndState:
    If <button is released> THEN
    DEBUG "Off",CR
    GOTO Initial
    ENDIF
    GOTO 2ndState

    The IF THEN ENDIF is used for illustration. You can do this with the BUTTON statement as well.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-11-26 09:00
    I think you can make BUTTON work the way you want by flip-flopping the targetstate:

    btnWrk VAR Byte
    targetstate VAR Bit
    Main:
      BUTTON 0, 1, 255, 0, btnWrk, targetstate, Off
      DEBUG "On", CR
      targetstate=0
    Off:
      DEBUG "Off" , CR
      targetstate=1
      GOTO Main
    

    There are several ways to do this, one of which Mike pointed out. It all depends on what else your program needs to do. Does it have to do other tasks at the same time as checking the button?
Sign In or Register to comment.