Shop OBEX P1 Docs P2 Docs Learn Events
Need help with pushbutton (Pull-down circuit) — Parallax Forums

Need help with pushbutton (Pull-down circuit)

ZackyoZackyo Posts: 3
edited 2014-12-12 09:28 in BASIC Stamp
Hi. I need some help with my project, as I've started on Electro High school and got a project to make a house with lights and indicators (LED, Piezo Speaker and Bi-Color). I know this is a pretty simple code, but i need some help to finish the coding.
I've got everything wired and stuff, but i dont know how to make a pushdown button be an "ON" and "OFF" button at the same time. If you click on the pushdown button, you will start the process, but if you press down the same button again, it will turn off the process, but i dont know how to do the "OFF" part. I think i need some variables, but dont know what to do. So if some of you could help me, and build the code up, i would really appreciate that.

I've made a little video, and I'm using an "Pushdown button", "Rele", "Piezo Speaker", "Bi-color" and two "LED/Lights":

https://www.dropbox.com/s/35i3nfwmx5vxo4o/Video%2011.12.14%2C%2016.14.02.mov?dl=0

Here's the code is use (I use the port 0 for the pushbutton):

' {$STAMP BS2}
' {$PBASIC 2.5}

DO
IF (IN0 = 1) THEN
DO
FREQOUT 1, 150, 4750
HIGH 6
LOW 5
PAUSE 500
HIGH 2
PAUSE 20
LOOP UNTIL IN0=1
ELSE
LOW 2
PAUSE 20
HIGH 5
LOW 6

ENDIF
LOOP

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2014-12-11 08:37
    It sounds like you want to have a "push on / push off" switch, push it once to turn on something then push it again to turn the something off. To do that, you need some way to remember the current state of the switch (on or off) and you need some way to remember what the previous state of the button was so you can see when the button was pushed rather than just pushed or held down. In other words, you want to see when the button was off and now it's on.

    Both variables would be bits since they just have to be on or off. You might have "OldButton var bit" as one variable and "ButtonState var bit" as the other. Both would be initialized to zero.

    You need a loop where you check stuff repeatedly. If OldButton is false (zero) and port 0 is true (one), then the button has been pressed and you toggle ButtonState (0->1,1->0). You also set OldButton to the value of port 0 (after testing it).
  • ZackyoZackyo Posts: 3
    edited 2014-12-11 22:16
    Yes, you're right. But i have issues on where to put The codes and what codes i really need to use. I'm using PBasic 2.5 as language, but are you able to set up a code for that? Cause i cant learn without doing it, heh.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-12-11 22:28
    Sure, whenever you get a pulse from the Input, the Basic Stamp will Toggle the state of an Output or some other condition that you create in the program. It is a big of added code, but that is exactly why you use a microcontroller -- for the memory.

    You might read up on TOGGLE in the BasicStamp Manual. There are code examples therein.
  • ZackyoZackyo Posts: 3
    edited 2014-12-11 23:49
    Do you have a link to it? We dont use books on the Microcontrollers anymore. And what codes should i have? I've got no clue at all, as I'm pretty new to this.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-12-12 08:11
    The BasicStamp Manual is available for free online as a pdf download.
    Just google "BasicStamp Syntax and Reference Manual" for a link.

    Also read and think about what was mentioned above about creating your main loop by Mike Green. It is important useful advise..

    If you are very, very new... google "What's a Microcontroller?" That book is also free to download in pdf form. It should make learning a lot easier.
  • Hal AlbachHal Albach Posts: 747
    edited 2014-12-12 09:28
    The PDF files Loopy referenced are also available from within the PBasic Editor, under "Help".
Sign In or Register to comment.