'this code takes 34 bytes
'change pin number for your processor type
symbol led_sw=pin1 'pin for the switch
symbol led_pin=2 'pin for LED
symbol led_bit=4 'bit for pin 2
symbol sw_on=1
main:
if led_sw=sw_on then switch_led
toggle 4
'do other stuff when switch isn't pressed
goto main
switch_led:
readoutputs b0
b0=b0 and led_bit
if b0=led_bit then 'LED is ON, turn OFF
'code when LED is OFF
low led_pin
else 'LED is OFF, turn ON
'code when LED is ON
high led_pin
end if
do while led_sw=sw_on loop 'loop until switch is released
'if you do a pause instead of this,
'holding down the button will
'make the light go ON,OFF,ON,OFF, ect.
goto main
Okay (got it )Westaust55,
That is what I thought it would do. I think the original question was if a push button could be made to work like an on/off switch. When you push the button switch it will toggle the LED then wait for you to release the button (do while button is pressed loop). When you press the push button again it will toggle the LED and then wait till you release the button. If you don't have the "do while button is pressed loop" and you just hold the button down, you will toggle on/off continuously. Am I right on that?
Thanks,
Clint
'Push-Push switch from momentary contact pushbutton.
'post #8
'http://www.picaxeforum.co.uk/showthread.php?t=11388#post88311
'this code takes 34 bytes
'change pin number for your processor type
symbol led_sw=pin1 'pin for the switch
symbol led_pin=2 'pin for LED
symbol led_bit=4 'bit for pin 2
symbol sw_on=1
main:
' if led_sw=sw_on then switch_led
' toggle 4
if led_sw=sw_on then gosub switch_led
'do other stuff when switch isn't pressed
goto main
switch_led:
readoutputs b0
b0=b0 and led_bit
if b0=led_bit then 'LED is ON, turn OFF
'code when LED is OFF
low led_pin
else 'LED is OFF, turn ON
'code when LED is ON
high led_pin
end if
do while led_sw=sw_on loop 'loop until switch is released
'if you do a pause instead of this,
'holding down the button will
'make the light go ON,OFF,ON,OFF, ect.
' goto main
return
setint %00000001,%00000001
'activate interrupt when pin0 only goes high
main:
'other code/program here
goto main
interrupt:
toggle 4 ‘ switch output 4 on
pause 500
if pin0 = 1 then interrupt 'loop here until the
'interrupt cleared
setint %00000001,%00000001 're-activate interrupt
return
if pin0 = 1 then interrupt 'loop here until the interrupt clears
do : loop until pin0 = 0 ; wait until switch goes low/off