Shop OBEX P1 Docs P2 Docs Learn Events
turn led on and off — Parallax Forums

turn led on and off

Rutcgr18Rutcgr18 Posts: 47
edited 2008-12-27 00:21 in BASIC Stamp
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

Comments

  • Rutcgr18Rutcgr18 Posts: 47
    edited 2008-12-25 21:26
    if the button is hit onces it turns led on. Then if the button is held for more than x seconds it will shut the led off
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-25 21:41
    Can you write a program that is a "push on" / "push off"? That's the first step you need to learn how to do.

    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?
  • William BrownWilliam Brown Posts: 9
    edited 2008-12-25 22:19
    Your if statement can be something like (pseudo code):

    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
  • Rutcgr18Rutcgr18 Posts: 47
    edited 2008-12-25 22:46
    Mike Green said...
    Can you write a program that is a "push on" / "push off"? That's the first step you need to learn how to do.

    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?
    I can do the two button setup already.· I just dont know how to use the count fuctiuon.· I am assuming i can count how long the button is held down
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-25 23:03
    What do you mean by "count function"?

    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?
  • ercoerco Posts: 20,256
    edited 2008-12-25 23:05
    One simple way: use out the TOGGLE command, which makes a high pin low and a low pin high. Then you'll monitor the pushbutton for a press and release sequence, then TOGGLE the LED pin and loop back.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • Rutcgr18Rutcgr18 Posts: 47
    edited 2008-12-25 23:33
    can anyone give me some sample code for this??
  • ercoerco Posts: 20,256
    edited 2008-12-25 23:39
    BS1 or BS2?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • Rutcgr18Rutcgr18 Posts: 47
    edited 2008-12-25 23:40
    bs2
  • ercoerco Posts: 20,256
    edited 2008-12-25 23:56
    Try this with a pushbutton on pin1 and an LED on pin2:

    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."
  • Rutcgr18Rutcgr18 Posts: 47
    edited 2008-12-26 01:27
    That worked perfect.· Now i know how to use toggle.· Also i ment to ask is there a way to tell if a pin is high or low?
    Thanks

    Post Edited (Rutcgr18) : 12/26/2008 1:36:19 AM GMT
  • ercoerco Posts: 20,256
    edited 2008-12-26 01:51
    Glad that worked for you.

    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
  • Rutcgr18Rutcgr18 Posts: 47
    edited 2008-12-26 02:44
    thanks
    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
  • Rutcgr18Rutcgr18 Posts: 47
    edited 2008-12-26 02:45
    i know its a lot but was a good learning for me.
    I will look into the out and in.
    Thanks for your
    help
    Mike
  • ercoerco Posts: 20,256
    edited 2008-12-26 03:45
    Mike: If you just hold your button on permanently, does your LED cycle on & off?

    erco

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • Rutcgr18Rutcgr18 Posts: 47
    edited 2008-12-26 04:38
    I am not sure i will have to try that. Ok i know i should know this awsnwer but i dont. Could you explan the diffence from VSS and VDD
    THanks
    Mike
  • MikerocontrollerMikerocontroller Posts: 310
    edited 2008-12-26 06:37
    ·· Vdd is the high side of the voltage supply and Vss is the low side.· In the case of the Stamp's requirements Vdd should be 5 volts and no higher.· The Stamp Vss voltage should be 0 and no less than that.
  • William BrownWilliam Brown Posts: 9
    edited 2008-12-26 23:32
    I've seen other markings in schematics that I was also curious about. I've concluded that "Vin" is unregulated voltage from the power source. Is this correct? What is Vcc?
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-12-27 00:21
    Yes, "Vin" is unregulated voltage from the power source. Vdd is the output of a regulator, on the BS2 that's 5 volts. Vss is "Ground".

    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.
Sign In or Register to comment.