How to stop program with button press
Spragmatic
Posts: 59
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
·········································· Greg
Comments
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.
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.
·
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!!
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··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 3/27/2010 2:07:49 PM GMT
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
·
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.
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?
·
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··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 3/27/2010 3:21:40 PM GMT
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!
What is the most important· Routine Loop that you have and what is the second and third and so on
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
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
Please Note that I have not tested this code to see how well it works
I hope this helps
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 3/28/2010 2:01:05 AM GMT
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
Greg
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Shawn Lowe
When all else fails.....procrastinate!
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}
Greg
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.
·
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.
·