BS1 One minute timer with a button
Luis_P
Posts: 246
in BASIC Stamp
Hello!
I need to run a timer for 1 minute and check if Pin 1 is high (button press). When Pin1 is high get out of the loop. Whats the best way to do this on the BS1 ????
So far I have this but is not accurate...
SYMBOL A = B2
FOR A = 0 TO 255 ' 1 minute
PAUSE 200
IF PIN1=1 THEN Routing1
NEXT
Thanks for any help!
Comments
256 * 200ms = roughly 51 seconds, a little short of 1 minute. If the 1 minute is crucial to your application, make the PAUSE a little longer. If the issue is the granularity of the test for the button press (200ms), make the size of the PAUSE smaller (like 100ms) and increase the loop size (like FOR A = 0 to 511). You'll have to make variable A a word instead of a byte.
Thanks Mike!
I've done lots of little industrial projects with the BS1. Note that this kind of programming
is dangerous for two reasons.
1) Best practice is to used named symbols. I suggest you do this instead.
2) Debouncing inputs can eliminate problems
True story: A professional haunted house in NYC was about to open and on their walk-through things went berserk. Why? Because any time someone keyed a radio, prop swould act as if they'd been triggered. When they showed me their code, it was looking at trigger inputs the same as you're doing. The key was to debounce the triggers. That solved the problem.
Here's routine that will let you delay up to 255 seconds and can be aborted by pressing a button (be sure to use a pull-down on that input) for 100ms. This may look a little scary at first, but I've deployed this code many times and it works well. Call with GOSUB Delay.
There is a hidden bit of trickery in this line:
The BS1 does not use operator precedence; it evaluates left-to-right. This line will add one to btnCount and then multiply the updated amount by the state of the override input, which will be 0 or 1. If the button is not pressed, that line will always evaluate as 0. When the button is pressed, btnCount can increment, and when it reaches 10 (100ms of loop time), the routine will abort.
By using the debounce code, you don't have to worry about electrical noise bouncing you out of your delay routine.
Jon, could you post that code with the declared variables on top of the code for me to understand what is what? I don’t think I will have any electrical noise but I will try your cose.
Please.
Thanks