On/Off bushbutton question
Dave45
Posts: 36
I must be really dense because I have been trying to figure out how to get a pushbutton to turn an led on and off. One push on, next push off. I have been trying to use the toggle command, I've checked out the button command, I'm having no luck. The button is active-low. Can someone please lead me in the right direction. So far I've got it to work randomly, but not consistantly.· Thanks.
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
an easy way would be
OUTPUT 5
DO
OUT5=~IN1
LOOP
the TOGGLE command would be
TOGGLE 5
japer
OS-X: because making Unix user-friendly was easier than debugging Windows
You need to alternate the LOGICAL FUNCTION of what the button press does within your progam. In other words, there must be 2 alternate paths through your program logic. One is taken on "push one" (odd pushes if you will) and the other is taken on "push two" (even pushes if you will). Presently there is only ONE logic path through your program.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
I'm not new to programming, but I am new to the Basic Stamp. So I made sure to run the code. It does toggle the LED each time the button is pressed.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
This is a single pass, non modal loop, so other code can go in line. The ledout line may need explanation. ^ is the XOR operator in PBASIC. btn ^ bt0 is 1 if and only if the past state and the present state are different. btn ^ bt0 & btn is 1 only if there is a change AND btn is 1, that is, the button has just been released. That, XOR'd with the current state of the ledout, causes ledout to toggle to the opposite state.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
BUTTON Pin, DownState, Delay, Rate, Workspace, TargetState, Address
DownState is where you specify whether the button is active-high or active-low. The button I'm using is active-low so DownState is 0.
TargetState is where you specify which button state you're looking for. This is independent of whether the button is active-high or active-low, so a 1 always means pressed and a 0 always means not pressed. This comes in handy if you ever change the type of button you use. You can do something like this:
Then if you switched to active-high buttons, you could just change "btnActiveState" to 1 and not have to make any change to the actual code.
With that in mind, the code should make more sense. The first BUTTON statement is looking for the button to be pressed (TargetState=1). When the button press happens, it branches to Toggle_LED and the LED switches state. Then there's a loop that waits for the button to be released (TargetState=0). When that happens, the program jumps back to Main and waits for the button to be pressed again.
My code is ok for seeing how the button command works, but in a real-world program Terry's code is much better. It doesn't require control of the program flow. (I think you'd need to debounce the button input but don't take my word for it--I'm just a beginner. )
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
yep your right SSteve trying a code before i tested is a bad idea.
any way for a simple solution one i used in a challenge
for Applied Sensor Chap # 2 Challenge 2 & 3
hopes this helps
"input or IN is pin #1 TOGGLE is Pin # 5 LED light"
DO
DO
LOOP UNTIL (IN1=0)
DO
LOOP UNTIL (IN1=1)
TOGGLE 5
LOOP
you can embellish with FREQOUT commands and DEBUG display if you
want to
japer