Shop OBEX P1 Docs P2 Docs Learn Events
Using the push button as a toggle. — Parallax Forums

Using the push button as a toggle.

robofan301robofan301 Posts: 4
edited 2013-05-24 04:39 in General Discussion
I am using the homework help board to make a car that will drive forward, using continuous servos.
Since the HHB does not have an on off switch, I decided to try to get it to read a pushbutton, connected to a port (Port 2).
I can program the logic if IN2 =1, then stop motors. But since the button is spring loaded, as soon as I release the button, it will read zero and the motors will start up again.
What I'm trying to accomplish is to have the motors toggle. If I press the button once, the motors stop, if I press it again the motors start up again.
Can anyone give me a clue, as to how to work this type of logic in?
Someone who programs in Java told me to use a Boolean, but it appears that PBasic doesn't have this declaration?
What's my best move?
Thanks

Comments

  • 4x5n4x5n Posts: 745
    edited 2013-05-20 16:47
    It's a rather common problem and the basic solution is simple. Watch the pushbutton and when it's pressed (I'm going to assume that it returns a "1") set a variable which will be watched by the mainline program. Continue to watch the pushbutton and note when it's released (0) now when the push button is pressed again (1) toggle the variable and wait until the pushbutton is released again (0). Of course you're going to have to deal with things like contact bounce, etc.

    Solution is simple but there are a number of different ways to do the nuts and bolts part depending on your application.
  • CuriousOneCuriousOne Posts: 931
    edited 2013-05-20 22:00
    Google for NE555 pushbutton switch.
  • robofan301robofan301 Posts: 4
    edited 2013-05-24 04:39
    Ok, Thanks for the feedback. I resolved the issue by declaring my counting variable as a "bit".
    Then I just incremented it by 1 every time the button was pressed.
    Thanks Again.
Sign In or Register to comment.