View Full Version : turning leds on and off
mertkon
- 25th April 2007, 01:18
hi i have just started to picbasic. i tried to control led by button but i could not be successful.
i wrote a code like this
DEFINE OSC 48
DEFINE RESET_ORG 800h
TRISB.3 = 0
TRISB.7 = 1
low portb.3
buttons:
if portb.7 = 0 then 'if button is pressed
high portb.3 'turn on led
endif
if portb.7 = 1 then
low portb.3
endif
goto button
end
but at this code only while i am pressing the button led turns on and when i pull my finger back from the button it turns off. but i want led to turn on up to next pressing of button. what should i do. thanks for help and sorry for my english
skimask
- 25th April 2007, 03:57
Read your code! It's doing exactly what you tell it to do!
----------------assume the button is pressed to start with
low portb.3 ----- starts off LED off
buttons:
if portb.7 = 0 then 'the button is pressed, light the LED
high portb.3
endif
if portb.7 = 1 then 'the button is pressed, so don't light the LED
low portb.3
endif
goto button
Now the other way around...assume the button is not pushed...........
low portb.3 ----- starts off LED off
buttons:
if portb.7 = 0 then 'the button is not pushed, so this if/then does not execute
high portb.3
endif
if portb.7 = 1 then 'the button is pressed, so light the LED
low portb.3
endif
goto button
And besides all that, at the end, don't you want to 'GOTO BUTTONS' instead of 'BUTTON'???
What you probably want is something like this:
DEFINE OSC 48
TRISB.3 = 0 : TRISB.7 = 1 : state var bit : state = 0 : portb.3 = state
buttons:
portb.3 = state 'turn the led on or off according to the state bit
if portb.7 = 1 then 'if button is not pressed, don't do anything
goto buttons
endif
if portb.7 = 0 then 'if the button is pressed
if state = 0 then 'if the LED was off last time...turn it on...
state = 1
else
state = 0 'otherwise turn it off (since it was on last time)
endif
endif
portb.3 = state 'turn the led on or off according to the state bit
pause 100
waitforrelease:
if portb.7 = 0 then
goto waitforrelease 'wait for the button to be released before checking it again
endif
goto buttons
end
Think about it for a bit...
sayzer
- 25th April 2007, 04:20
Try this, too.
<font color="#000000"><b>.
..
...
</b><font color="#0000FF"><b>LED </b></font><font color="#000080"><b>VAR </b></font><font color="#0000FF"><b>PORTB</b></font><b>.</b><font color="#FF0000"><b>3
</b></font><font color="#0000FF"><b>Btn </b></font><font color="#000080"><b>VAR </b></font><font color="#0000FF"><b>PORTB</b></font><b>.</b><font color="#FF0000"><b>7
</b></font><font color="#0000FF"><b>LED </b></font><b>= </b><font color="#FF0000"><b>0
</b></font><font color="#0000FF"><b>Start</b></font><b>:
</b><font color="#000080"><b>IF </b></font><font color="#0000FF"><b>Btn </b></font><b>= </b><font color="#FF0000"><b>0 </b></font><font color="#000080"><b>THEN
WHILE </b></font><font color="#0000FF"><b>Btn </b></font><b>= </b><font color="#FF0000"><b>0 </b></font><b>: </b><font color="#000080"><b>WEND
TOGGLE </b></font><font color="#0000FF"><b>LED
</b></font><font color="#000080"><b>ENDIF
GOTO </b></font><font color="#0000FF"><b>Start
</b></font>
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.