turn led on and off
I want to to make a simple way to turn on and off a led.· I am just learing.· I can turn it on wiht an if statement but i want to shut it off with the same button.· I would like it so you have to hold the button to turn it off.·
Thanks
Mike
Thanks
Mike
Comments
If you can't do that yet, how about a program with two buttons (on two I/O pins) where one button turns the LED on and the other button turns it off?
main loop of program
if button pressed then gosub buttonpress
end main loop
buttonpress:
if int_of_led_pin = 1 then 'Check the pin to see if it's "on" (high)
low pin 'if it is, turn it off
else
high pin 'if it's not, turn it on
endif
end sub
The "push on" / "push off" case uses two "states". These are two separate loops in the program. One is the initial loop where the program waits for the button to be pushed, then lights the LED. The 2nd state is a loop where the program also waits for the button to be pushed, but here it turns the LED off. Once the program turns the LED on, it goes to the start of the 2nd loop (changes state). When the 2nd state loop turns the LED off, it goes to the start of the 1st loop (changes state again). Each state needs to start with a sort of substate where it waits for the button to be released if it's still pushed from the previous action, then a second substate where it waits for the button to be pushed. If you're using the BUTTON statement, this (in a loop) takes care of the two substates.
What sort of states do you think you might need for the "push on" / "push and hold off" situation?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
WAITHIGH: IF IN1=0 THEN WAITHIGH' wait for pin1 to go high (button pushed)
TOGGLE 2' toggle LED
PAUSE 200' debounce delay (adjust this value to suit)
WAITLOW: IF IN1=1 THEN WAITLOW' wait for pin1 to go low (button released)
PAUSE 200' debounce delay
GOTO WAITHIGH' loop back
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
Thanks
Post Edited (Rutcgr18) : 12/26/2008 1:36:19 AM GMT
To answer your last question: Yes, but now you gotta do the legwork! I gave you a Christmas freebie in the spirit of the season, but you would have learned more if you would have dug it up yourself, right? Look up OUTS & INS to answer your question from the WAM book or BS Syntax manual. Cheers!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
Post Edited (erco) : 12/26/2008 2:21:35 AM GMT
i just came up with this
seems to work
Poweron:
IF (IN3=1) THEN
HIGH 14
PAUSE 250
GOTO Poweroff
ELSE
GOTO Poweron
ENDIF
Poweroff:
IF (IN3=1) THEN
LOW 14
PAUSE 250
GOTO Poweron
ELSE
GOTO Poweroff
ENDIF
I will look into the out and in.
Thanks for your
help
Mike
erco
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
THanks
Mike
Vcc is used in TTL circuitry, where Vdd is used in CMOS circuitry, but basically they mean the same thing in the BS2 world -- regulated +5 volts.