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

BUTTON workspace

MachineMonkeyMachineMonkey Posts: 30
edited 2005-07-02 19:20 in BASIC Stamp
I need to re-evaluate the state of an input some time after BUTTON is invoked. In other words, if a user pushes the button for 2 seconds, I want to ignore it, but if the button iis pressed for 20 seconds, I want to run a secondary routine and output change.
So...in a perfect world, the 'workspace' parameter would continue to update throughout the runtime of the program and I could just check in on the flag state at appropriate intervals.
I'm not near my Stamps at this point and won't be for a few days. Any chance a few here already know the answer to this one?

Thanks,
Kalo

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-02 15:38
    BUTTON does not work that way; PBASIC is single-threaded and the workspace variable is only updated when you're actually running the BUTTON instruction.

    You can solve this by using a task manager framework and polling the button periodically.· Your button scan would increment a counter when pressed, or clear the count to zero when released.· Perhaps something like this:

    Button_Scan:
    · IF (BtnInput = Pressed) THEN
    ··· btnCount = btnCount + 1
    · ELSE
    ··· btnCount = 0
    · ENDIF
    · RETURN···

    Now you have to drop this into a framework that allows you to poll it.· I do this:

    Main:
    · GOSUB Button_Scan
    · IF (btnCount >= BtnMax) THEN
    ··· ' do something
    · ENDIF
    · ON task GOSUB Task1, Task2, Task3, Task4
    · task = task + 1 // 4
    · GOTO Main

    Now... the trick is to keep all the tasks running at the same speed so that the button is scanned at the same rate.· That's not always possible.· What you can do, then, is measure the timing of each task (use a 'scope or another BASIC Stamp) and add the amount of time required for the routine.· Let's say your Task1 code takes about 25 millisecods; you'd set a variable to 25 like this:

    Task1:
    · ' task 1 operational code
    · timeInc = 25
    · RETURN

    Since the structure of the main program causes the button to be scanned between tasks, you can update it like this:

    Button_Scan:
    · IF (BtnInput = Pressed) THEN
    ··· btnCount = btnCount +·timeInc
    · ELSE
    ··· btnCount = 0
    · ENDIF
    · RETURN

    Now you'll update your main program to respond to the value of btnCount passing some threshold.· So, you can do what you want to do, but it's going to take more than one line of code....
    ·


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-07-02 19:20
    Hello,
    ·· I guess maybe this won't help you for the durations you're talking about, but I did write a small piece of code just now on my PDB to detect whether a button is being pressed/released or held down using the BUTTON command.· I had a BS2p plugged in when I did this, but it will work on any BASIC Stamp Module.
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    btnWrk    VAR   Byte
    Main:
    DO
      BUTTON 0, 0, 200, 20, btnWrk, 0, Main
      IF btnWrk = 200 THEN
        DEBUG "Pressed", CR
      ELSEIF btnWrk = 20 THEN
        DEBUG "Held Down", CR
      ENDIF
      PAUSE 200
    LOOP
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.