need a help for pushbutton code that suits my program
sushiandmorihiko
Posts: 40
hello,guys
i need a help in writing a code for this case:
i have a main program which contain 2 subroutine, in the main program, i wish to be able to shift from one subroutine to the other by the use of the pushbutton. how to write the program in the main and then in the subroutine in order for it to work well.
i tried something like this:
DO
GOSUB firstsubroutine
IF IN4=1 THEN GOSUB secondsubroutine
LOOP
firstsubroutine:
bla bla
bla bla
IF in4=1 THEN RETURN
secondsubroutine:
bla bla
bla bla
IF in4=1 THEN RETURN
i know there is something wrong in these code--or wrong concept but i am really stuck,pls help me fix this if any1 knows.
thank you in advance
best regards
i need a help in writing a code for this case:
i have a main program which contain 2 subroutine, in the main program, i wish to be able to shift from one subroutine to the other by the use of the pushbutton. how to write the program in the main and then in the subroutine in order for it to work well.
i tried something like this:
DO
GOSUB firstsubroutine
IF IN4=1 THEN GOSUB secondsubroutine
LOOP
firstsubroutine:
bla bla
bla bla
IF in4=1 THEN RETURN
secondsubroutine:
bla bla
bla bla
IF in4=1 THEN RETURN
i know there is something wrong in these code--or wrong concept but i am really stuck,pls help me fix this if any1 knows.
thank you in advance
best regards
Comments
Also make sure to say whether any behaviors are "edge sensitive", in other words, the response is to the button going from off to on or on to off.
there are:
-one main rountine
-2 subroutine
-1 pushbutton
what i wanted to do it to move from 1 rountine to the other by using the pushbutton.
so it works like this:
when i press the button physically ,then lets say the controller is executing the first routine, it will then move to the second rountine immediately and vice versa.
i am using a pushbutton not a switch so i think it needs somekind of relay kind of code for it remember the value of the pushobutton because you only push them once for a short while.
thanks mike green.i am really stuck in this,i have been figuring this out for almost 2hours
can anyone pls tell me how to latch a value, i am using a pushbutton,so when i press it will be 1 but when i let go it will be 0, so i need a code to latch this think--so that when i press one,it will stay 1 until i press again then it will turn to 0.
thank you in advance
'·· Do any other initialization here
·· goto state1
prestate1:
·· if in4 = 1 then goto prestate1 ' wait for button to be released
'· Put code here that should only be executed once when the program switches to state 1
state1:
·· if in4 = 1 then goto prestate2 ' switch to state 2
'··Do whatever the first routine has to do as long as it's in state 1
·· goto state1
prestate2:
·· if in4 = 1 then goto prestate2 ' wait for button to be released
' Put code here that should only be executed once when the program switches to state 2
state2:
·· if in4 = 1 then goto prestate1 ' switch to state 1
'· Do whatever the second routine has to do as long as it's in state 2
·· goto state2
it really works man,big thanks for you.
i knew it somehow from the beginning there was something wrong with my code as when i pressed the button,the execution is faster than my hand letting it go,so it didnt give the output i desired.
thats why like in mike green's code,it fixed the biggest trick with the "prestate"
i can try the whole day but without this problem fixed,i am down to nothing--my eyes hurt after figuring it out for more than 3 hours.
thanks man--i ll try putting my 2 big subroutines in now,,see if it works.
lastly,you are indeed an expert
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Use this circuit to latch button press (gotta love this chip):
Use this code to read buttons - don't have to repeatedly read button!
' {$STAMP BS2}
' {$PBASIC 2.5}
HIGH 1
INPUT 2
Main1:
IF IN2=1 THEN··· 'button was latched
··· PULSOUT 1,10 'reset latch
··· GOTO Main2·· 'goto othe routine
endif
·· 'do all your code for this routine here
GOTO Main1
'
Main2:
IF IN2=1 THEN··· 'button was latched
··· PULSOUT 1,10 'reset latch
··· GOTO Main1·· 'goto othe routine
endif
·· 'do all your code for this routine here
GOTO Main2
END
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 10/12/2007 10:56:03 PM GMT
you are right man, its much simpler to just use a swith that hold its position by itself.nevertheless,i really want to try something new and the i only have pushbutton with me play with anyway it took me really long to figure it out until mike green figured out for me.anyway,that idea of technorobbo looks quite complex for just latching a pushbutton, is that chip an op amp?
Don't let the schematic scare you, it's far from complex.·Note the schematic only requires·1 pullup resistor to make it work, the rest is jumpers and a button. You should be able to throw this on a proto board in 5 minutes.
Here's some info on the chip.
http://en.wikipedia.org/wiki/555_timer
http://www.kpsec.freeuk.com/555timer.htm
Parallax sell them too
http://www.parallax.com/detail.asp?product_id=604-00009
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
thanks for the advice,Technorobbo.
BtnWrk VAR Byte
BtnFlip VAR Bit
DO
Main Routine Stuff
Main Routine Stuff
Main Routine Stuff
BUTTON <pin>, <downstate>, 255, 0, BtnWrk, 0, SkipBtn 'here we branch only when the button is not pressed, in order to skip over the next line of code
BtnFlip = BtnFlip + 1 'This will cause BtnFlip to toggle between 0 and 1 when you press the button
SkipBtn: 'If the button does not get pushed then we skip over the BtnFlip command
IF BtnFlip = 0 THEN 'You could also replace all this with a BRANCH instruction, but you would need some means of returning back into the DO...LOOP for it to run correctly.
GOSUB SubRoutine0
ENDIF
IF BtnFlip = 1 THEN
GOSUB SubRoutine1
ENDIF
LOOP
SubRoutine0:
Stuff
Stuff
Stuff
RETURN
SubRoutine1:
Stuff
Stuff
Stuff
RETURN
There are things that could be done perhaps to optimize the execution speed of the code, depending on how you want your subroutines to execute. This is one example that should work nicely. Remember all RAM is cleared to 0 at reset, so you can always expect SubRoutine0 to execute first. Pressing the button will flip the state of BtnFlip, causing SubRoutine1 to execute. Pressing it again will cause SubRoutine0 to execute once again.