Shop OBEX P1 Docs P2 Docs Learn Events
need a help for pushbutton code that suits my program — Parallax Forums

need a help for pushbutton code that suits my program

sushiandmorihikosushiandmorihiko Posts: 40
edited 2007-10-14 16:31 in BASIC Stamp
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-11 22:52
    I'm not sure what you want to do with the pushbutton or with the subroutines. How about describing the behavior you want in a little more detail with regards to the button push and the Stamp's behavior?

    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.
  • sushiandmorihikosushiandmorihiko Posts: 40
    edited 2007-10-11 23:33
    here is the idea mike green,

    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
  • sushiandmorihikosushiandmorihiko Posts: 40
    edited 2007-10-11 23:45
    and perhaps this will make it easier:

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-11 23:47
    initialization:

    '·· 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
  • sushiandmorihikosushiandmorihiko Posts: 40
    edited 2007-10-12 00:52
    my salute for mike green,

    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
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-10-12 07:55
    The easier solution is to use a maintained contact switch rather than a momentary one. Push it once, it's ON. Push it a second time it's OFF. It doesn't get much simpler than that, and no involved programming is required to use it.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • D FaustD Faust Posts: 608
    edited 2007-10-12 18:47
    There is a BUTTON command that should handle a momentary button nicely.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    LOOKDOWN ThisThread, [noparse][[/noparse]Your_?, My_?, Cool_Thing], looknum
    LOOKUP looknum, [noparse][[/noparse]1, 2, 3], subnum
    ON subnum GOTO Hope_this_helps, Thanks!, WOW!!
    END 
    
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-10-12 22:51
    Here's a variation on Bruce Bates' Idea.

    Use this circuit to latch button press (gotta love this chip):

    attachment.php?attachmentid=49852

    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
    655 x 343 - 21K
  • sushiandmorihikosushiandmorihiko Posts: 40
    edited 2007-10-13 04:25
    thanks for the ideas guys,

    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?
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-10-13 04:46
    No it's not an omp amp, the 555 is a timer chip available everywhere since the 1970's.· They're extremely inexpensive. It can be used for a great deal of circuits.· This circuit uses the chip as a 1-bit flip-flop but the chip has many uses.

    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
  • sushiandmorihikosushiandmorihiko Posts: 40
    edited 2007-10-13 05:12
    oohh,,sorry,i thought i was an op-amp--if i look at it again-it is sure quite simple and straightforward--hhmm indeed2 i will try one day if i have the chip.
    thanks for the advice,Technorobbo.
  • JSWrightOCJSWrightOC Posts: 49
    edited 2007-10-14 16:31
    Let's not forget the BUTTON command, which was designed specifically for this type of thing. I would do something like this:

    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.
Sign In or Register to comment.