Qs about the BUTTON command
stleric
Posts: 13
Is there a post or document that (better) explains the 'button' command? I'm finding the explanation given in the manual unclear in a couple of respects. First how do the 'downstate' and 'targetstate' parameters and the actual voltage at the pin all relate to each other? The manual refers to the button "being pressed" but that could go either way depending on how the port pins are wired up. For instance here's my little test program:
Now, when the program starts, depending on how my switch is set up and the values of the two state params I get an erroneous execution of the "DEBUG "0", CR" command.
My second question is how long is the cycle time that's used in computing the delay and autorepeat times? I'm using a BS2, but in general, what is it?
TIA,
eric
' {$STAMP BS2} ' {$PBASIC 2.5} btnWrk VAR Byte delay CON 255 btnWrk = 0 PAUSE 5000 DEBUG "z", CR DO BUTTON 0, 1, delay, 255, btnWrk, 0, Nothing0 DEBUG "0", CR Nothing0: BUTTON 1, 1, delay, 255, btnWrk, 0, Nothing1 DEBUG "1", CR Nothing1: BUTTON 2, 1, delay, 255, btnWrk, 0, Nothing2 DEBUG "2", CR Nothing2:
Now, when the program starts, depending on how my switch is set up and the values of the two state params I get an erroneous execution of the "DEBUG "0", CR" command.
My second question is how long is the cycle time that's used in computing the delay and autorepeat times? I'm using a BS2, but in general, what is it?
TIA,
eric
Comments
Thanks for the tip. Before it was good enough but it works much better with your suggestion.
My first question still stands though (I think).
Thanks again,
eric
Thanks---
In answer to your question about the cycle and auto-repeat times, it is in terms of iterations, not absolute time. The BUTTON command will always be enclosed in some sort of loop that will execute repeatedly. Suppose the delay is set to 15 and rate is set to 3. Once the button is pressed, that loop has to execute 15 times before the repeat starts (to execute the target action), and after that, the loop has to execute 3 times between actions. So the time completely depends on how long it takes to execute the loop (plus the target action when it is invoked). The article linked explains (to my satisfaction at least!) how that is accomplished under the hood.
You didn't show your complete loop in your example, but I assume that it is there with multiple button commands. As Mike pointed out, you need a separate work variable byte for each button. Also, in your test, you are disabling both the delay and rate by setting them equal to 255.
You have downState=1 and targetState=0. That makes the button active high (down=high when pushed), but the action (DEBUG) execute when the button is not down. The targetState parameter is 1 when you want the action executed when the button is in its down state (whether that be low or high).
See this is my problem, you can wire the port so it's normally at VDD or normally at VSS and when you push the button the port goes to (roughly) VSS or VDD respectively. Do they mean DownState = 1 -> positive logic at the pin and DownState = 0 -> negative logic?
I just skimmed that page you linked while writing this, looks like there's some stuff there I should reread. The part about debouncing reminds me of what they say about laws and sausages.
Thanks,
eric
Now this is clear. They should incorporate this in the refman, I have v2.2 (which I think is current) and they allude to it in a diagram. I think they should state it more explicitly.
Thanks again,
eric
As noted at the link, there is no debouncing in the wider sense, where it would take multiple samples at the same state before it was called stable in the state. The BUTTON command does not do that. One sample at each level is all it takes. On the other hand, there are at least a few milliseconds (which can be adjusted) in between samples in a loop, and the interval tends to swamp the bounce time of most switches.
I don't see much point in using the BUTTON command unless you need the delay/repeat features. It uses a whole byte for the state variable, when a single bit will do. I have examples of that on the page referenced.
Did some more reading and I just discovered that it's possible to access the ports directly so I'm with you, I don't see any real use for the BUTTON command at least for my purposes. So to hell with the BUTTON command.
Thanks again,
eric