Need help with pushbutton (Pull-down circuit)
Zackyo
Posts: 3
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
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
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).
You might read up on TOGGLE in the BasicStamp Manual. There are code examples therein.
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.