LED ON and OFF with 2 Push button switches


Closed Thread
Results 1 to 16 of 16
  1. #1
    Join Date
    Apr 2013
    Posts
    3

    Default LED ON and OFF with 2 Push button switches

    Hi Guys I am new to picbasic. I wrote a small program to control a LED with 2 pushbutton switches. The one to switch it on and the other one to switch it off. I can get it to switch it one but when I tried the 2nd button to switch it off it just dim.

    Please help

    My code:

    LED Con 0 ' Alias GPIO.0 to LED
    PB Var GPIO.3 ' Alias GPIO.3 to push button
    PB1 Var GPIO.4 ' Alias GPIO.4 to push button

    ANSEL = 0 ' Set all digital
    CMCON = 7 ' Analog comparators off

    ' Button press turns on LED (MCLRE must not be enabled)
    mainloop:
    If PB = 1 Then ' If button pressed...
    Low LED ' Turn on LED
    Else
    High LED ' Turn off LED
    Endif

    If PB1 = 0 Then ' If button pressed...
    Low LED ' Turn off LED
    Endif

    Goto mainloop ' Do it forever

    End

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    Hi,
    Read thru your mainloop, line by line, and try to figure out what's happening.

    You check if PB is pressed, if it is you turn on the LED - that's fine. But if PB isn't pressed you turn off the LED - that's not what you want, is it?
    Do you have the two switches wired the same way? I ask because you check PB for a logic 1 while you check PB1 for a logic 0 - that's all fine if it's the way you have it wired, don't forget pullup/pulldown resistors though.
    Finally, the comments are somewhat confusing because at one place you say that LOW LED turns the LED on while in another place you say that LOW LED turns the LED off - I don't know which is correct.....

    /Henrik.

  3. #3
    Join Date
    Dec 2011
    Location
    IO93ok
    Posts
    190


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    LED CON 0 doesn't clearly map the LED to a pin.

    LED VAR GPIO.0 does. Just easier to understand.


    I like to put a pinout connection diagram for the chip as a reminder what is connected to what. It also helps when asking a question.
    Similar to this example

    Code:
    ;
    ; Target Controller -      PIC16F628A 
    ;                                  __________  
    ;                       d6--RA2 |1       18| RA1--d5
    ;                       d7--RA3 |2       17| RA0--d4
    ;              LED-------RA4 |3       16| OSC1--------
    ;     +5V-----------!MCLR |4       15| OSC2--------
    ;     Ground----------Vss |5       14| VDD---------+5 V
    ;              gps in-----RB0 |6       13| RB7--------- 
    ;                         ---RB1 |7       12| RB6--------- 
    ;                         ---RB2 |8       11| RB5--e
    ;                   pbutt--RB3 |9       10| RB4--rs
    ;                                     ----------
    Last edited by tasmod; - 28th April 2014 at 13:33.
    Rob.

    The moment after you press "Post" is the moment you actually see the typso

  4. #4
    Join Date
    Apr 2013
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    Hi Guys thanks for all the inputs.
    I actually need to use a pic like the 16f128 etc because the actual application has 4 inputs (in other words 4 inputs that will give me a 5v high signal independent from each other). Once any of these inputs are triggered a corresponding LED must then switch on.(in other words input 1 must switch on LED 1, input 2 must switch on LED 2, input 3 must switch on LED 3, input 4 must switch on LED 4) I then need to have 4 separate push button switches to cancel the corresponding LED's - one for each LED.
    So I was trying to use the 12f675 just to see if I can get it to work with one push button as an input to switch-on a LED and then another pushbutton to switch-off the LED but am battling to get it right.
    Any help will be appreciated

    Thanks a lot

    Willem

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    Hi,

    > Any help will be appreciated

    In an effort help you understand what's wrong with your original code I tried to give you some advice in my previous response but apparently that didn't help, so....

    For two buttons and one LED:
    Code:
    Main:
    
      If PB1 = 1 THEN   ' Button 1 is pressed 
        HIGH LED1   ' Turn on LED 1
      ENDIF
    
      IF PB2 = 1 THEN    ' Button 2 is pressed
        LOW LED1    ' Turn off LED 1
      ENDIF
    
    Goto Main
    That's it, rinse and repeat for as many buttons and LEDs as you want.

    /Henrik.

  6. #6
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    I don't comprestand why you're using a PIC. Transistors or relays will be easier than a PIC for just switching on and off LEDs.

    If you're stuck on using a PIC I'm betting you could use a 12F675 and I'm wondering if this will work. Why can't you make a voltage divider and set a pin at 1/2 of VDD which should be ambiguous with the port set to digital. Then you could monitor the same input pin for a high or low. This way you could use two pins for four inputs and have the other 4 outputs free for your LEDs. Well, you only need 3 ports to switch 6 LEDs but that's another thread.

    Caveat is I've never tried it and wonder if it will work. If I get time I'll throw it together today and see if it works.

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    Have a look to Microchip DS 40040 ... ( tips and tricks for 8 pins ...)

    solutions are there !

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    Thanks for reminding me about that document. Tons of useful information.

  9. #9
    Join Date
    Sep 2015
    Posts
    17


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    Hi Acetronics2 and AvionicsMaster1,
    Would either of you guys please post the .pdf for DS40040 from Microchip. It sounds like a must have document, but I cannot find it on Microchip's website.
    Thank you.
    Regards,
    mike

  10. #10
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches


  11. #11
    Join Date
    Sep 2015
    Posts
    17


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    Hi fratello,
    Thanks for the link.
    Regards,
    mike

  12. #12
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    It’s a flip-flop, so if you do it in software a flip-flop might as well be 1 bit of memory.
    You’re going to want to look a the LED in a real program and know if it’s on or off,
    so will usually want to store the LED state which would usually represent some program state.

    Code:
    flipflop var bit ‘ 1 = LED is on 0 = LED is off
    debounce var byte ‘ button debounce counter
    flipflop = 0 ‘ start with LED off at power up, or on if you want to
    debounce = 0 ‘ reset debounce counter
    
    trisb.0 = 1’ button pin input
    trisb.7 = 0’ LED pin output
    
    cycle: 'program cycle
    
    if debounce > 199 then
    if portb.0 = 1 then
    flipflop = 1
    debounce = 0
    else
    flipflop = 0
    debounce = 0
    endif
    endif
    
    if flipflop = 1 then ‘ set LED to flip flop state
    portb.7 = 1
    else
    portb.7 = 0
    endif
    
    if debounce < 200 then ‘ increment button debounce counter
    debounce = debounce + 1
    endif
    
    goto cycle ‘ repeat loop forever

  13. #13
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    But that's only using 1 button Art not 2 as in the title of the thread.....
    Dave Purola,
    N8NTA
    EN82fn

  14. #14
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    Quote Originally Posted by Dave View Post
    But that's only using 1 button Art not 2 as in the title of the thread.....
    Whoops!
    No need to accommodate contact bounce for the buttons then.

    Code:
    flipflop var bit ‘ 1 = LED is on 0 = LED is off
    flipflop = 0 ‘ start with LED off at power up, or on if you want to
    
    trisb.0 = 1’ buttonA pin input ON
    trisb.1 = 1’ buttonB pin input OFF
    trisb.7 = 0’ LED pin output
    
    cycle: 'program cycle
    
    if portb.0 = 1 then
    flipflop = 1 ‘ set memory bit
    endif
    if portb.1 = 1 then
    flipflop = 0 ‘ reset memory bit
    endif
    
    portb.7 = flipflop ‘ set LED on or off
    
    goto cycle ‘ repeat loop forever

  15. #15
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    Now, Thats nice and easy. The only thing I would change is not having a variable for the state of the output pin but reading the output state.
    Dave Purola,
    N8NTA
    EN82fn

  16. #16
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: LED ON and OFF with 2 Push button switches

    In the long run, for midrange pics that don’t have a port latch register,
    we should be using a byte port latch to avoid RWM error if time allows.
    So it would go, write single bit to port latch byte, write entire port latch byte to port, read back bit from port latch byte.
    Not that I can say I’ve ever encountered a problem I’d put down to RWM error.

Similar Threads

  1. Push button on PIC16F887
    By menta in forum General
    Replies: 21
    Last Post: - 25th June 2008, 07:47
  2. 12f629 push button & LED
    By yankee in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th February 2006, 01:45
  3. Button Push within 3 second Window
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd December 2005, 10:06
  4. push button problem
    By ngeronikolos in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 2nd June 2005, 19:44
  5. Push Button 'Menu' to Choose Variable.
    By Tissy in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 10th March 2005, 07:00

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts