PDA

View Full Version : LED ON and OFF with 2 Push button switches



Willemdt
- 28th April 2014, 06:08
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

HenrikOlsson
- 28th April 2014, 06:21
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.

tasmod
- 28th April 2014, 13:25
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




;
; 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
; ----------

Willemdt
- 28th April 2014, 20:11
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

HenrikOlsson
- 28th April 2014, 20:42
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:


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.

AvionicsMaster1
- 28th April 2014, 22:02
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.

Acetronics2
- 29th April 2014, 10:31
Have a look to Microchip DS 40040 ... ( tips and tricks for 8 pins ...)

solutions are there !

Alain

AvionicsMaster1
- 29th April 2014, 13:28
Thanks for reminding me about that document. Tons of useful information.

mikejp56
- 31st October 2015, 15:19
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

fratello
- 31st October 2015, 17:42
http://ww1.microchip.com/downloads/en/DeviceDoc/40040c.pdf
Regards !

mikejp56
- 2nd November 2015, 23:11
Hi fratello,
Thanks for the link.
Regards,
mike

Art
- 5th November 2015, 12:45
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.




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

Dave
- 5th November 2015, 16:54
But that's only using 1 button Art not 2 as in the title of the thread.....

Art
- 5th November 2015, 17:38
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.



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

Dave
- 6th November 2015, 11:40
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.

Art
- 12th November 2015, 16:58
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.