Shop OBEX P1 Docs P2 Docs Learn Events
How to stop program with button press — Parallax Forums

How to stop program with button press

SpragmaticSpragmatic Posts: 59
edited 2010-03-28 16:13 in BASIC Stamp
I am having a hard time trying to figure out how to stop my program.· Once I press my pushbutton it starts the program.· I would like to be able to·press the·pushbutton again anytime·after the initial start of the program and have it stop and do something else.· I've been working on this issue for awhile now and there has to be a simple way?· ·Any ideas and help would be greatly appreciated!!· Thanks!!·

·········································· Greg

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-03-27 01:52
    It depends on your program. You probably have a test for the pushbutton being pressed somewhere early in your program after the initialization and before the "main stuff" is done. You need another similar test in all the places where you want to interrupt the rest of the program and that depends on how you've structured the program.

    You could also connect your pushbutton to the reset line and use an EEPROM location to count how many times the Stamp has been reset and use that to control whether the program continues onward or just waits for the next reset to occur. The disadvantage of stopping the program this way is that the program can be interrupted no matter what it's doing and, if you've got something complicated attached to the Stamp, it may be left in a state that's harder to reinitialize.
  • bill190bill190 Posts: 769
    edited 2010-03-27 01:53
    *which* button are you pressing?

    The reset button on the board?

    Or a button you have added?

    Also might help to say what type of board you have and include a copy of your code.
    ·
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-27 02:02
    Mike-
    I don't think I'll use the reset line. That is simple, but like you said it will stop the whole program and I just need a section stopped with the press of the button. Thanks!!

    bill190-
    It's a pushbutton I've added. I've looked at a few threads here and there are some great examples. I just can't get them to work for some reason. I am using RCTIME for a long 24sec delay in the section of code I need to be able to stop. That might be the reason I'm having issues. I'm actually using the SX28 with SX/B, but have had alot of good help on this forum. Thanks!!
  • bill190bill190 Posts: 769
    edited 2010-03-27 02:14
    If your code is scanning the button to see if it is pressed, try holding down the button for a long time. Perhaps the processor is busy doing something else at the "instant" you press the button? (Can only do one thing at a time.)
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-27 03:00
    What is a long time? I need it to be a short as possible on the button press.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-03-27 03:53
    The program has to test for the button being pressed. The test will succeed only if the button is being pressed when the test is done. If the program is busy doing something else, it can't test the button. You will need some kind of "memory" otherwise to remember that the button was pressed until the program can notice it. This can be done with a flipflop, but that requires a separate I/O pin to reset the flipflop. It can be done by charging or discharging a sufficiently large capacitor with the pushbutton and sensing the voltage on the capacitor with the Stamp I/O pin. The Stamp can reset the capacitor after sensing whether the button's been pushed.
  • ScopeScope Posts: 417
    edited 2010-03-27 08:59
    Isn't it possible, and often advantageous, to have one of the Propeller cogs "on watch" for button, or other sensory input, while the other seven cogs are actively engaged with . . . whatever it is they're asked to do?
  • sylvie369sylvie369 Posts: 1,622
    edited 2010-03-27 09:40
    Scope said...
    Isn't it possible, and often advantageous, to have one of the Propeller cogs "on watch" for button, or other sensory input, while the other seven cogs are actively engaged with . . . whatever it is they're asked to do?
    That'd work fine if he were using a Propeller, but he posted this in the Basic Stamp forum, so we're assuming that's what he's using. Since the Stamp can only do one thing at a time, it can't check for button press at the same time as it's doing other code.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-03-27 13:48
    You could use DO WHILE or LOOP UNTIL

    in the routine with your RCTIME

    If you post your code that you are using I can see if there is a work around

    How ever ..... >>>>>>

    Mike Green said

    The test will succeed only if the button is being pressed when the test is done. If the program is busy doing something else, it can't test the button.

    You will need to hold the button long enough for your routine to see that the button has been pushed

    But like every one has said the Basic Stamp can only do one thing at a time




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 3/27/2010 2:07:49 PM GMT
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-27 14:15
    I'm actually using the SX28 with SX/B, but have had alot of good help on this forum.

    Thanks everyone for the response!!

    sam_sam_sam-
    · Here is the code--· I need to be able to stop the program and then do something else·during RCTIME on the (Close) section of code.· I think the problem is that the RCTIME is 1-24sec long.· I have the Pot2 set for the max (24sec) also.· Should I just get rid of the RCTIME and use PAUSE 24000 instead?· Would it then be easier to stop the PAUSE?· Hope that made sense?· Thanks!!
    ············· Greg

    ·
  • bill190bill190 Posts: 769
    edited 2010-03-27 14:40
    If the delay is 24 seconds, hold the button down for longer than 24 seconds and see what happens.

    Also you can place the following in your start loop...

    DEBUG "start"

    This will display a zillion start messages each time the program is in the start loop. And this is when it is checking to see if the button has been pressed.

    So when it would be displaying these start messages, that is when you could press the button and it would then see that the button was pressed.
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-27 14:50
    bill190-

    That's where the problem lies. I need to be able to stop the program instantly at the button press. Anywhere within that 24sec period. This in turn sends HIGH to one of my relays. I'm starting to think that I might not be able to accomplish this to my needs? So that I get this straight-- If the microcontroller is doing something, whether it be a PAUSE statement or HIGH/LOW statement then you have to wait until that command is done before it can do another?
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-03-27 14:55
    I need to be able to stop the program and then do something else·during RCTIME on the (Close) section of code.
    ·
    The only way to do this is to have counter that loop and when the counter reaches that then do something else

    or ·what·bill190·is talking about in his post bellow

    Here in one example of how you check your input and still have a pause with out using the PAUSE Command


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    laps VAR word

    DO WHILE IN0 = 0
    ······························ ' Your code here if you want to do any like make output go HIGH or LOW
    laps = laps + 1

    IF laps = 700 THEN······· EXIT
    ···························· or
    ································· GOSUB Routine
    loop

    ······························ ' Your code here· what is next



    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 3/27/2010 3:21:40 PM GMT
  • bill190bill190 Posts: 769
    edited 2010-03-27 15:02
    Instead of pausing and doing nothing, the computer *could* be doing something useful like checking the buttons to see if they were pressed.

    And this could be an endless loop.

    Or if could be a FOR loop which is limited in the amount of loops it does. This would be a delay as well.

    But first I suggest you stick that DEBUG message in your start loop and see what is happening with your current code. And also see what happens when you hold down the button for a long time. Good learning to understand how it is working as it is now. See what the problem is. Learn how to troubleshoot!
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-27 15:49
    I understand that I need a Do...Loop or FOR...Next but not sure how to place it in the code above to work? Thanks!!
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-03-27 17:36
    OK

    What is the most important· Routine Loop that you have and what is the second and third and so on

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-27 17:49
    Sam-

    In the attached code above I need to add a counter in place of RCTIME(pretend it is not there) in the Close: routine that counts 24sec. As long as PButton2 is not pressed again it will go through entire Close: routine. If PButton2 is pressed again anytime in the 24sec count sequence it would stop and then Relay3 would go HIGH PAUSE 700 LOW. This would then send you to Open: only. Do not want Close: working until Open: again. I hope all this made sense. Thanks!!

    I've tried all kinds of examples here in the forums and got it to work somewhat.· It just won't count for the duration I need for some reason.

    I need to do this after PButton2 is pressed-
    ··· HIGH Relay3
    ··· PAUSE 700
    ··· LOW Relay3
    ··· HIGH Led2

    (Time for 24sec) or count I mean

    then-
    ····LOW Led2
    ··· HIGH Relay4
    ··· PAUSE 700
    ··· LOW Relay4
    ··· PAUSE 50

    If I·have to press the PButton2 during the 24sec it will stop the routine and then do what I stated above.· Thanks!!


    Post Edited (Spragmatic) : 3/27/2010 7:43:15 PM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-03-28 01:22
    ·Try this and see if this will work for you
    Please Note that I have not tested this code to see how well it works
    I hope this helps

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    Relay3    PIN  0
    Relay4    PIN  1
    Led2      PIN  2
    
    Laps   VAR   Word
     
     
    Main :
     
       laps = 0000
     
    DO WHILE IN3 = 0
    
        LOW Led2
        HIGH Relay4
        Laps = Laps + 1
        LOW Relay4
        PAUSE 50
    
        IF laps = 2400 THEN Main
     
        DEBUG HOME, DEC ? Laps  
    
    LOOP
     
        laps = 0000
     
    DO
        LOW Led2
        HIGH Relay3
        Laps = Laps + 1
        LOW Relay3
        PAUSE 50
     
        DEBUG HOME,DEC ? Laps  
     
     LOOP UNTIL laps = 2400 OR IN3 = 1 
                                         ' <<<<<<  You could put GOTO main or a routine here
     
    
     
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 3/28/2010 2:01:05 AM GMT
  • bill190bill190 Posts: 769
    edited 2010-03-28 01:24
    Might experiment around with something like the following. Adjust the 30000 amount up/down to add or·subtract delay.

    Loop_Count·· VAR···· Word·········· ' Value can be 0 to 65535
    Loop_Count = 0

    · FOR Loop_Count = 0 TO 30000······ ' count from 0 to max of 65535
    ····· IF PButton2 = 0 THEN Close
    ····· '[noparse][[/noparse]other stuff you want to do here]
    · NEXT

    END
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-28 01:51
    Thanks guys!! I will work with these and see what happens. I really appreciate the help!! Starting to get excited now! smile.gif

    Greg
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-28 15:21
    For some reason I can't get my VAR to count. I've tried many versions of the examples above. I must be missing something. I've got the code to work, but with no timing between sections?
  • Shawn LoweShawn Lowe Posts: 635
    edited 2010-03-28 15:34
    Is this code for a BS2 stamp or a SX chip?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    When all else fails.....procrastinate!
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-28 15:42
    SX28
  • bill190bill190 Posts: 769
    edited 2010-03-28 15:47
    ' {$PBASIC 2.5}

    Loop_Count·· VAR···· Word·········· ' Value can be 0 to 65535
    Loop_Count = 0

    · FOR Loop_Count = 0 TO 30000······ ' count from 0 to max of 65535
    ······ DEBUG DEC Loop_Count, " "

    · NEXT

    END

    Create a new separate program with just the above in it. Then run it and you will see it counting up in the debug window.

    And click on the appropriate stamp so that goes at the top like...

    ' {$STAMP BS2}
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-28 15:58
    Thanks everyone! I got it! Finally smile.gif I had things in the wrong order. I will let everyone know once project is finished. Thanks again!!

    Greg
  • bill190bill190 Posts: 769
    edited 2010-03-28 16:02
    Then the trick to programming is to understand what each command does and how it works.

    Read the help page on the "FOR" command.

    After FOR is a variable called "Loop_Count"

    Understand that variables are like a storage space. You say ahead of time how much space you will be using. If you try using more space than you have allocated, there will be problems. Loop_Count is a "Word" size variable. Below are the different sizes you can have...

    mouse VAR Bit··· ' Value can be 0 or 1.
    cat VAR Nib··· ' Value can be 0 to 15.
    dog VAR Byte··· ' Value can be 0 to 255.
    rhino VAR Word··· ' Value can be 0 to 65535.

    So be sure the use the correct size variables for what you are doing.

    Then next is "= 0 TO 30000". This counts from 0 to 30000. You could start counting at 5 and only count up to 10. Then it would be "= 5 TO 10".

    Then next is the space for what you want to do each time it loops. In this case, we display the current count with DEBUG.

    Last is the word "NEXT". This sends it back up to the FOR line and increases the count 1. Then it loops again.

    ·
  • bill190bill190 Posts: 769
    edited 2010-03-28 16:13
    Spragmatic said...
    Thanks everyone! I got it! Finally smile.gif I had things in the wrong order. I will let everyone know once project is finished. Thanks again!!

    Greg
    Great!

    That's another thing with programming. You must be perfect. And us humans tend to make mistakes every now and then!

    One comma in the wrong spot and all sorts of problems can occur. This is *real* fun if you have about 30 pages of code and need to track down that errant comma!

    The secret to this it to be able to "see" what is going on. Use DEBUG to display the value of a variable, the count of something, or to see if the program is ever getting to a certain point.

    When I am troubleshooting, I'll stick DEBUG messages all over the place so I can "see" what is going on as the program executes. Double check that things are doing what they should be doing.
    ·
Sign In or Register to comment.