Shop OBEX P1 Docs P2 Docs Learn Events
Controlling and LED on/off with one push button — Parallax Forums

Controlling and LED on/off with one push button

compucompu Posts: 5
edited 2013-08-14 10:36 in BASIC Stamp
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-09 21:01
    First you go through the "What's a Microcontroller?" tutorial that you can download from Parallax here. There's a section (Chapter 2) that deals with turning an LED on and off and a section (Chapter 3) that deals with pushbuttons. I believe there's an example in Chapter 3 of exactly what you want. Have a look.

    If you have the Stamp Editor, you may already have a copy of the tutorial in the Editor's help files.
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-01-09 21:03
    One of two things come to mind:

    #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!
  • compucompu Posts: 5
    edited 2011-01-10 14:39
    Can you please tell me the exact code
    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
  • compucompu Posts: 5
    edited 2011-01-10 14:40
    can you please tell me the exact code if possible sir
    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
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-10 15:57
    Usually when people ask for "exact code", they have a project for school and are trying to avoid having to learn how to program in order to do their assignment. We won't help you there. Read the tutorial I recommended. If there is something in the tutorial that you don't understand, please ask for help understanding. If you try something in the tutorial and it doesn't work the way you think it should, explain what you've tried and we'll help you get it working, but we won't do your work for 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.
  • compucompu Posts: 5
    edited 2011-01-10 17:54
    I am sorry mate
    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
  • Desy2820Desy2820 Posts: 138
    edited 2011-01-10 19:02
    Suggested code is below. I haven't tested this, so be prepared to test and adjust.
    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
  • compucompu Posts: 5
    edited 2011-01-10 19:14
    Thank you so much mate
    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 ?
  • Desy2820Desy2820 Posts: 138
    edited 2011-01-10 20:11
    Yes, the TOGGLE command will cause the LED to turn on, then off, then on (since it starts out as off). Toggle reads the current state, high or low, then changes it to the opposite. If it was low, it goes high, if high it goes low.

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-10 21:20
    The BUTTON statement as given by Desy2820 won't work because BUTTON does a GOTO to Press, not a subroutine call (GOSUB). The Stamp will probably reset when it does the RETURN since there's nothing to return to. Something like this should work:


    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
  • bsnutbsnut Posts: 521
    edited 2011-01-10 23:38
    I am in agreement with Mike Green on both of his posts. That now said, you should download the information and the help file that is with the Basic Stamp Editor program. There is example program code for every instruction/command for the Basic Stamps.

    Here is some code to get you started.
    Main:
    If IN0 = 1 THEN Ledon  'Goto label Ledon when PIN 0 is true
    GOTO Main  ' Goto label Main when PIN 0 is false
    
    Ledon:
    TOGGLE 1  'Toggle led on and off on PIN 1
    GOTO Main  'Goto Main 
    
    This should give some idea what you need to do.
  • ercoerco Posts: 20,250
    edited 2011-01-11 09:44
    simplifies to:

    main:if in0=0 then main
    toggle 1:pause 500:goto main

    the pause 500 gives you 500 ms to release the pushbutton.
  • wespiecewespiece Posts: 6
    edited 2013-05-21 12:24
    along these same lines - I am building an electric car controller for lights and accessories. I have the code below to toggle lights on and off, but if wanted to make Output 0 - toggle on and off AND flash when it was On what code would I need to add. I want to create a flasher circuit basically, with a toggle input thru a PC connection. Code is below:'
    {$STAMP BS2e}
    ' {$PBASIC 2.5}
    ' Hook up a two LEDs to Pins 3 and 4 to test this.
    ' Then VB code is set up for Com1 incase you have to change this to the Com
    ' port of your computer.
    command VAR Byte
    SI PIN 1
      Main:
      SERIN 16, 16468, [STR command\1]    ' Get 1-byte string
    
    CheckCommand:
      IF command = "1" THEN
         GOTO Task1
      ENDIF
      IF command = "2" THEN
         GOTO Task2
      ENDIF
      IF command = "3" THEN
         GOTO Task3
      ENDIF
      IF command = "4" THEN
         GOTO Task4
      ENDIF
    GOTO Main
    
    Task1:
    TOGGLE 0
    GOTO Main
    Task2:
    LOW 0
    GOTO Main
    Task3:
    TOGGLE 1
    GOTO Main
    Task4:
    LOW 1
    GOTO Main
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-05-22 00:46
    wespiece wrote: »
    I have the code below to toggle lights on and off, but if wanted to make Output 0 - toggle on and off AND flash when it was On what code would I need to add.

    I don't use the Basic Stamp much myself, but I think the following code should do what you want.
    {$STAMP BS2e}' {$PBASIC 2.5}
    ' Hook up a two LEDs to Pins 3 and 4 to test this.
    ' Then VB code is set up for Com1 incase you have to change this to the Com
    ' port of your computer.
    
    
    command VAR Byte
    onFlag VAR Nib
    flashPhase VAR Nib
    loopCount VAR Word
    FLASH_LOOPS CON 1000 ' Change this value to adjust blinking rate
    SI PIN 1
    
    
    Main:
      SERIN 16, 16468, [STR command\1]    ' Get 1-byte string
    
    
    CheckCommand:
      IF command = "1" THEN
         GOSUB Task1
      ELSEIF command = "2" THEN
         GOSUB Task2
      ELSEIF command = "3" THEN
         GOSUB Task3
      ELSEIF command = "4" THEN
         GOSUB Task4
      ENDIF
      GOSUB CheckFlash
    GOTO Main
    
    
    Task1:  ' toggle blinking LED on or off
      IF onFlag = 0 THEN ' if turning on, setup flash
        onFlag = 1       ' variable used to keep track
        flashPhase = 1   ' of flash state
        loopCount = 0
      ELSE
        onFlag = 0
      ENDIF
    RETURN
    
    
    Task2: ' turn off blinking LED
      onFlag = 0
      LOW 0
    RETURN
      
    Task3:
      TOGGLE 1
    RETURN
      
    Task4:
      LOW 1
    RETURN
    
    
    CheckFlash:
      IF onFlag = 1 THEN           ' Do we need to worry about flashing?
        IF loopCount = FLASH_LOOPS ' Should LED state change yet?
          loopCount = 0            ' Reset counter.
          IF flashPhase = 1        ' Which phase were we just in?
            flashPhase = 0         ' Change phase and LED state.
            LOW 0
          ELSE
            flashPhase = 1
            HIGH 0
          ENDIF
        ELSE    
          loopCount = loopCount + 1 ' No phase change. Just add to counter.
        ENDIF
      ENDIF
    RETURN  
    

    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.
  • wespiecewespiece Posts: 6
    edited 2013-08-14 07:08
    getting an error on the last else statement - else must be proceeded by If or Case ELSE...
    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
  • Mike GreenMike Green Posts: 23,101
    edited 2013-08-14 07:33
    Should be: IF loopCount = FLASH_LOOPS THEN

    Similarly: IF flashPhase = 1 THEN
  • wespiecewespiece Posts: 6
    edited 2013-08-14 07:49
    modified it to look like this - but still getting the error shown aboveCheckFlash:
    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
  • Mike GreenMike Green Posts: 23,101
    edited 2013-08-14 08:08
    Please read the chapter on the IF statement in the Basic Stamp Syntax and Reference Manual or in the Stamp Editor's help files. The IF statement has two forms:
    1) IF <expression> THEN <statement>
    
    2) IF <expression> THEN
          <statement>
       ELSE
          <statement>
       ENDIF
    
    You can't combine the two forms, so "loopCount = 0" has to be on a separate line. Similarly "flashPhase = 0" has to be on a separate line.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-08-14 10:36
    wespiece wrote: »
    still getting the error shown aboveCheckFlash:

    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.
  • compu wrote: »
    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

    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
  • ercoerco Posts: 20,250
    Or else, use nothing but an on/off pushbutton switch and save your $50 BS2 for a more interesting project.
  • gbell wrote: »
    compu wrote: »
    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

    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!


Sign In or Register to comment.