Shop OBEX P1 Docs P2 Docs Learn Events
On/Off bushbutton question — Parallax Forums

On/Off bushbutton question

Dave45Dave45 Posts: 36
edited 2006-04-01 04:42 in Learn with BlocklyProp
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

  • Dave45Dave45 Posts: 36
    edited 2006-03-31 02:19
    Guess I need to learn how to type too. turn.gif
  • SSteveSSteve Posts: 808
    edited 2006-03-31 02:40
    Were you waiting for the button to be released? Try this:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' ------------------------------------------------------------
    ' Constants
    
    LEDOut        PIN        0
    ButtonIn        PIN        1
    
    ' ------------------------------------------------------------
    ' Variables
    
    btnVar        VAR        BYTE        ' workspace for BUTTON command
    
    
    ' ------------------------------------------------------------
    ' Program Code
    
    Main:
        BUTTON ButtonIn, 0, 255, 0, btnVar, 1, Toggle_LED
        PAUSE 50
        GOTO Main
    
    Toggle_LED:
        TOGGLE LEDOut
    
    Wait_For_Release:
        BUTTON ButtonIn, 0, 255, 0, btnVar, 0, Main
        GOTO Wait_For_Release
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • japerjaper Posts: 105
    edited 2006-03-31 03:47
    hello Dave45
    an easy way would be

    OUTPUT 5
    DO
    OUT5=~IN1
    LOOP

    the TOGGLE command would be

    TOGGLE 5

    japer
  • SSteveSSteve Posts: 808
    edited 2006-03-31 03:57
    But that would make the light go on when the button is down and off when the button is up, wouldn't it? That's not what he was asking.
    Dave45 said...
    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.
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-03-31 04:11
    Steve -

    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 -->
  • SSteveSSteve Posts: 808
    edited 2006-03-31 04:35
    Yes there's only one path, but the one path toggles the state of the LED. Isn't that what the OP was asking for?

    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
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-03-31 06:07
    Here it is written as a simple state machine:

    bt0 VAR bit   ' past state of button
    btn VAR bit   ' immediate state of button
    ledout PIN 1  ' output  for led
    btnin PIN 0    ' input for button
    
    LOW ledout   ' make led a low output
    bt0 = btnin   ' read initial state of button
    DO
      btn = btnin    ' read immediate state of button
      ledout = btn ^ bt0 & btn ^ ledout  ' toggle on transition
      bt0 = btn     ' immediate state becomes past state
    LOOP
    



    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
  • Dave45Dave45 Posts: 36
    edited 2006-03-31 11:07
    I think part of my problem is understanding exactly how OUTS and DIRS work. Could someone explain in simple terms how they work? Thanks for all the replys.
  • Dave45Dave45 Posts: 36
    edited 2006-03-31 11:40
    Thanks SSteve. Your program works nicely. Could you explain it so that I can follow what is going on. I know what the commands do, I guess I just don't follow how the flow of the program works.·Why is the TargetState 1 in the first button and not 0? They way I understand it, 0 should close the switch to make the led light up. I don't understand why the LED is off to begin with, then with the button command TargetState 1 makes·it comes on.
  • SSteveSSteve Posts: 808
    edited 2006-03-31 16:05
    Dave45 said...
    Why is the TargetState 1 in the first button and not 0? They way I understand it, 0 should close the switch to make the led light up.
    That tripped me up at first, too. Here's the syntax of the button command:

    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:
    btnActiveState        CON        0
    
    BUTTON btnPin, btnActiveState, 255, 0, btnVar, 1, Main
    


    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. smile.gif )

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • japerjaper Posts: 105
    edited 2006-03-31 23:40
    hello Dave45

    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
  • Dave45Dave45 Posts: 36
    edited 2006-04-01 04:42
    Thanks for all the help everyone.
Sign In or Register to comment.