Controlling and LED on/off with one push button
compu
Posts: 5
I want to control an LED with a push button
So that when I press the button the LED is on and when i press the button again the LED turns off
Can anyone give me the code for it please
thank you
really appreciate if you can help
I am using basic stamp 2
micro controller
So that when I press the button the LED is on and when i press the button again the LED turns off
Can anyone give me the code for it please
thank you
really appreciate if you can help
I am using basic stamp 2
micro controller
Comments
If you have the Stamp Editor, you may already have a copy of the tutorial in the Editor's help files.
#1 Use a toggle pushbutton instead of a momentary
IF IN0=1 then OUT1,1 'where your button is on pin0 and your LED is on pin1
#2 Use the "TOGGLE" BASIC Stamp command
IF IN0=1 THEN TOGGLE 1 'where your button is on pin0 and your LED is on pin1
In both cases you will likely need a debounce timer. The BASIC Stamp has an instruction just for this, although I've never used it; Look up the "BUTTON" command in your manual.
Good luck!
with a do loop, for next , picking a variable , if its needed
My push button is connected to IN1
and my LED is connected to P15 soo
would it be like
Do
IF ? THEN
?
ELSE ?
HIGH ?
LOW ?
PAUSE
LOOP
My push button is connected to IN3
and my LED is connected to P14
Do i need a "Do Loop" ???/
can yu plz tell me the exact code for it
thank you
Look at the Stamp Editor's help files for descriptions of each of the statements. HIGH and LOW require a pin number like "LOW 5" to make I/O pin 5 low. There's a space between the LOW and the pin number. You've noticed that you can use the value of an I/O pin as a variable like "IN7" to test the state of I/O pin 7. There's no space between the IN and the pin number in this case.
I tried reading it in chapter 3 and also chapter 2
I ve tried the pull up resistor , and also toggle command but no LUCK I GUESS
and by the way FOI its not a school project/assignment
I am trying to make a remote controlled car for my 6 year old son
I ve completed everything but i want to know how to use the push button
The situation is like when i push the button the green LED should turn on and the car should start , when i press it again the LED should turn off and the car should stop
It you can please help me AT LEAST for my 6 year old kid who's birthday is just coming up
I would appreciate it Thank you
I'm hoping this code gives you a good starting point.
Good Luck!
' ================================================== =======================
'
‘ {$STAMP BS2}
‘ {$PBASIC 2.5}
'
' ================================================== =======================
'
[ Program Description ]
‘ Toggles an LED on and off with a button press. Button must be active-high.
'
'
[ I/O Definitions ]
LED PIN 14 ‘ Your LED on pin 14. Use a series resistor to limit current. Other end of LED to ground.
Btn PIN 3 ‘ Your active-high button wired per Basic Syntax manual, pg 139 or 141.
'
[ Constants ]
Tgt_State CON 1 ‘ Specifies that the button will be high when pressed
'
[ Variables ]
btnWrk VAR Byte
'
[ Initialization ]
btnWork = 0 ‘ Set variable for BUTTON command to zero
OUTPUT LED ‘Make the LED pin an output
LOW LED ‘ Turn off the LED
INPUT Btn
'
[ Program Code ]
Main:
DO
PAUSE 5
BUTTON Btn, Tgt_State, 255, 5, btnWrk, 1, Press
LOOP
Press:
TOGGLE LED ‘Turn LED on and off with button press.
RETURN
END
I tried using codes like
If .... THEN...... ELSEIF........
how can i play around with these types ? any idea
by the way
from this code
If i press the push button the LED will turn on and when i press it again it will turn of right ?
If the LED doesn't light at first, try reversing the leads. I've driven myself nuts forgetting that they are polarity senstitive--and wondering why I can't get it to turn on.
For IF, THEN, ELSE, I'd try the example code in the Basic Stamp Syntax and Reference book. I also can't recommend enough the "What's a MicroController" (WAM) text. It really helped me get started, now I'm so rusty I probably need to work through it again to blow out the cobwebs.
Syntax Manual (5th from the top within table): http://www.parallax.com/tabid/440/Default.aspx
WAM: http://www.parallax.com/Portals/0/Downloads/docs/prod/edu/28123-WAM-v3.0.pdf
I hope this helped.
DO
PAUSE 5
BUTTON Btn, Tgt_State, 255, 5, btnWrk, 1, Press
GOTO keepGoing
Press:
TOGGLE LED ‘Turn LED on and off with button press.
keepGoing:
LOOP
Here is some code to get you started. This should give some idea what you need to do.
main:if in0=0 then main
toggle 1:pause 500:goto main
the pause 500 gives you 500 ms to release the pushbutton.
I don't use the Basic Stamp much myself, but I think the following code should do what you want.
Adjust the value of "FLASH_LOOP" to change the blinking rate.
I changed many of the "GOTO" statements to "GOSUB" and added "ELSEIF" statements instead of just "IF" statements. These changes make the program call "CheckFlash" on each loop.
Let me know how/if it works.
I don't have the Basic Stamp software installed on this PC so I haven't tested it.
loopCount = loopCount + 1 ' No phase change. Just add to counter.
ENDIF
ENDIF
RETURN if I chop out this statement it works but the led does not flash and a 1 toggles the led - 2 turns it on and 1 turns it off again thx for the help here
Similarly: IF flashPhase = 1 THEN
IF onFlag = 1 THEN
IF loopCount = FLASH_LOOPS THEN loopCount = 0
IF flashPhase = 1 THEN flashPhase = 0
LOW 0
ELSE
flashPhase = 1
HIGH 0
ENDIF
ELSE
loopCount = loopCount + 1 ' No phase change. Just add to counter.
ENDIF
ENDIF
RETURN
wespiece,
Sorry about having so many errors in the program I posted. I don't use Basic Stamps much and I didn't even have the editor installed on my computer at the time or I would have at least checked the code to see if it compiled.
IIRC, I was trying to illustrate how you'll need to monitor the state of the LED over multiple loops.
It looks like Mike Green has you pointed in the right direction on how to fix the syntax.
Well, here's your code:
' {$STAMP BS2}
' {$PBASIC 2.5}
Btn PIN 0
BTNwrk1 VAR Byte
START:
brite:
DO
HIGH 15
HIGH 14
HIGH 13
BUTTON btn, 0, 255, 20, BTNwrk1, 1, offf
LOOP
offf:
DO
LOW 15
LOW 14
LOW 13
BUTTON Btn, 0, 255, 20, BTNwrk1, 1, brite
LOOP
Welcome to the forums!
The original Poster has not been around since 2011, so I do not think he will be seeing your post, but thanks for posting!