View Full Version : a switch and 2LED
win_832001
- 11th October 2006, 09:50
Hello there..Here I need a solution regarding to my programming..It seems like have no problem with the programming flow.but it is still does not working as I want. Here the description and sequence that I want:
1) PIC circuit have a switch and 2 LED
2) Initially, the redLED flashed
3) After click pressed button the green LED will be flashed,
4) After that we pressed again the red LED will be flashed again.
5) The procees will continue according to the button is pressed.
<code>
GreenLED VAR PORTB.3
RedLEd VAR PORTB.4
emergency VAR PORTB.5 'switch pin
main :
IF emergency = 1 Then ' switch pin = 5V(high)
GoSub onn
Else ' switch pin = 0V(low)
GoSub offf
EndIF
GoSub main
onn:
High GreenLED
Low RedLED
Return
offf:
Low GreenLED
High RedLED
Return
</code>
mat janssen
- 11th October 2006, 10:01
@ I gave an answer here for another topic. sorry
sayzer
- 11th October 2006, 10:51
Assuming that you already set your pins as inputs & outputs; a code as below should work.
edit : Also, is it a push-pull button or a holder switch?
OUTPUT PORTB.3 'make pin an output pin
OUTPUT PORTB.4 'make pin an output pin
INPUT PORTB.5 'make pin an input pin
GreenLED VAR PORTB.3 'assign a name to the pin
RedLEd VAR PORTB.4 'assign a name to the pin
Emergency VAR PORTB.5 'switch pin
Begin:
GreenLED = 0 'Initially OFF.
RedLEd = 1 'Initially ON.
main :
IF Emergency = 1 Then 'Someone pushed the button.
WHILE Emergency = 1 : wend 'Wait for finger release.
TOGGLE GreenLED 'Change the status.
TOGGLE RedLEd 'Change the status.
ENDIF
GOTO main
mister_e
- 11th October 2006, 16:38
i'm not 100% sure of what you need but try this one..
TRISB=%00100000
emergency VAR PORTB.5 'switch pin
WichPORTB_Bit var byte
Loop var word
LedState var bit
wichPORTB_Bit=4
ledstate=1
Start:
For loop=0 to 500
if emergency=1 then
PORTB.0(wichportb_bit)=0
wichportb_bit=wichportb_bit ^7
while emergency : wend
pause 20
endif
PORTB.0(wichportb_bit)=ledstate
pause 1
next
ledstate=ledstate^1
goto start
ErnieM
- 11th October 2006, 23:06
3) After click pressed button the green LED will be flashed,
4) After that we pressed again the red LED will be flashed again.
This is a bit vague. As sayzer asks what kind of switch? A standard pushbutton doesn’t latch, so it only indicates a “1” while pressed, and “0” when released.
A pushbutton that latches will cost more then a button that doesn’t remember it’s state.
So.. to implement the cheaper switch you’ll need to A) debounce it and 2) remember the previous state.
Debounce simply means read it several times to insure you have a stable state. Typically a switch will only bounce in one direction, meaning it bounces ( rapidly closes and opens several times) when pressed but breaks cleanly; however, this may change over a lot or over the lifetime of the device, so best to debounce both ways.
I usually debounce a switch by reading it every 5 ms for 10 times. If I get the same reading every time, I accept the reading. If I get a different reading in any sample, I throw them all away and start over. This has worked well for me for the most part, the switch still responds quickly (typ 50 ms or so) so the person pressing it gets good feedback, and slow enough to get around any bounces.
Remembering the previous state just means saving it in a variable.
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.