BUTTON workspace
MachineMonkey
Posts: 30
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
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
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
·· 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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com