SW var GPIO.1
1. LED1 var GPIO.2
2. OnOff var bit
3. Swset var bit
4. Startup:
5. Led1=0
6. OnOff=0
7. GOTO Main
8. Main:
9…….
10…..do regular code stuff
11……
12. if OnOFF=1 then
led1=1
else
led1=0
endif
13. GOSUB Debounce:
14. GOTO Main
Debounce:
If SW=0 then
Swset=0 ;used to determine if switch is held down
RETURN ;if switch is not pressed return to main
Endif
If swset=1 then RETURN ;this returns to the main loop if SW is held down
If SW=1 then PAUSEUS 100 ;if switch is pressed pause 100us
IF SW=0 then RETRN ;if switch is now not pressed return to main
IF SW=1 then
OnOff=OnOff+1 ;if switch is still pressed toggle OnOff
Swset=1 ;used to determine if switch is held down
Endif
RETURN
Ok, Lets look at your code. The First goto Main (Line 7) is worthless... your program will automatically fall through to the beginning.
Lets Look at something similar...
SW var GPIO.1
LED1 var GPIO.2
OnOff var bit
Swset var bit
Startup:
Led1=0
OnOff=0
Main:
…….
…..do regular code stuff
……
if SW=1 then
led1=1
else
led1=0
endif
Pauseus 1000
Goto Main
Looking at this, as long as OnOff button is pushed (On or equal to 1) your LED will stay on. (assuming you are pulling the OnOff pin to ground, and when you push the button, you are at 5 volts).
Another way...............
SW var GPIO.1
LED1 var GPIO.2
OnOff var bit
Swset var bit
Startup:
Led1=0
OnOff=0
Main:
…….
…..do regular code stuff
……
if SW=1 then
Toggle Switchset
pauseus 100 ; debounce timing
endif
If SwitchSet=1 then
led=1
else
led=0
endif
Goto Main
This one, will keep the LED on until you press the switch the second time.
Realize, that I have not tested these programs, but it will give you an idea...
Also, make sure you correctly assign the GPIO pins and turn off things like comparitors... else your program will not work!
Dwayne
Ability to Fly:
Hurling yourself towards the ground, and missing.
Engineers that Contribute to flying:
Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute
Pilots that are Flying:
Those who know their limitations, and respect the green side of the grass...
Bookmarks