MrDEB
Well-Known Member
IMO I have tooo many IF THEN statements but this code works as is.
can it be done better or??
Always room for improvement.
// My objective is to monitor 10 switches using an 18F4520 pic. This is just
// a testing routine to determine if the sequence of events will work
// leds are OFF until the designated switch is depressed then stay ON until the designated switch is depressed again.
// can't help it but I feel this code could be better?? tooo many if thens??
//my plan is to designate each switch and led (swt1 and led1)
DEVICE = 18F2420
CLOCK = 8
INCLUDE"InternalOscillator.bas"
INCLUDE "SetDigitalIO.bas"
// alias to port pin...
DIM LED AS PORTc.4
dim swt as portc.3
low (led)
// main program...
while true
if swt = 1 and led = 0
then
led = 0
end if
if swt = 0 and led = 0 // button press
then
led = 1
delayms(500) // switch debounce
end if
if led = 1 and swt = 1
then led =1
end if
if led = 1 and swt = 0
then led = 0
delayms(500)
led = 0
end if
wend
can it be done better or??
Always room for improvement.
// My objective is to monitor 10 switches using an 18F4520 pic. This is just
// a testing routine to determine if the sequence of events will work
// leds are OFF until the designated switch is depressed then stay ON until the designated switch is depressed again.
// can't help it but I feel this code could be better?? tooo many if thens??
//my plan is to designate each switch and led (swt1 and led1)
DEVICE = 18F2420
CLOCK = 8
INCLUDE"InternalOscillator.bas"
INCLUDE "SetDigitalIO.bas"
// alias to port pin...
DIM LED AS PORTc.4
dim swt as portc.3
low (led)
// main program...
while true
if swt = 1 and led = 0
then
led = 0
end if
if swt = 0 and led = 0 // button press
then
led = 1
delayms(500) // switch debounce
end if
if led = 1 and swt = 1
then led =1
end if
if led = 1 and swt = 0
then led = 0
delayms(500)
led = 0
end if
wend