Hold button for a reset.

Jaden

Senior Member
Hi,
Little snippet I got working, please suggest improvements :) . Hope it is helpfull to someone.

I need to have one switch have a second function, and that is to reset something. SO I need to have that button held down for 3 seconds to then register a reset or high (can then be associated to an output pin). I have a led in to show that the high has been sent, also handy to put next to the button to show the operator that the reset has been sent! It will then go back and wait for another reset. I am using an 18M2. I have a LED on B.2.
 

Attachments

Last edited:

nick12ab

Senior Member
Since there's a PAUSE 3000, the program cannot do anything else and if the button is only pressed briefly, you still have to wait the full 3 seconds but since only one timer is available, you wouldn't want to use that. You should use a loop that does pause 100 30 times and check the button after every pause.
 

Jamster

Senior Member
A bit more efficient (lets you do other thing whilst checking):
Code:
	#picaxe 18M2
	SYMBOL resetcount=b0
	SYMBOL timetoreset=10

main:	'main part of program here
	
	if pinB.0=1 then 
		inc resetcount
	endif
	if pinB.0=0 then
		resetcount=0
	endif
	if resetcount=timetoreset then
		goto reset
	endif
	goto main

reset:     'reset routine here
             goto main
 

Jaden

Senior Member
Thanks for the feedback.

Good point Nick12ab about the pause taking the button away for that time! I should have realised as I had this issue in another bit of code I did!

Thanks for the inprovements Jamster, A bit more advanced than the level I am at but I will print it of, implement it and study what you have done.

Thanks all.
 

Jamster

Senior Member
Sorry,

Quick step by step:

Code:
#picaxe18M2
-simply tells the simulator which chip to simulate (ignore it)

Code:
SYMBOL  resetcount=b0
SYMBOL timetoreset=10
-means that b0 is written as 'resetcount' and 10 can be written as 'timetoreset' in the program.

Code:
if pinB.0=1 then 
        inc resetcount
endif
-if the B.0 pin is high then add 1 to 'resetcount' (b0)

Code:
if pinB.0=0 then
        resetcount=0
endif
-if the B.0 pin is released then set 'resetcounter' (b0) to 0

Code:
if resetcount=timetoreset then
        goto reset
endif
-if the person has pressed the button for 10 ('timetoreset') loops of the main program ('resetcount') then goto subroutine reset.


Hope this makes the program easier to read,
Jamster

P.S. If you want more information on a command have a look in manual 2.
 
Last edited:

westaust55

Moderator
Note that RESET is a command for X1, X2 and M2 parts and thus should not (for earlier parts) and cannot (for newer parts) be used as a label.

try this version:
Code:
#picaxe 18M2
SYMBOL resetcount=b0
SYMBOL timetoreset=10

main:	'main part of program here
	
	if pinB.0=1 then 
		inc resetcount
		if resetcount=timetoreset then 
			GOSUB resetcntr
		endif
	else
		resetcount=0
	endif
	goto main

resetcntr:     'reset routine here
            RETURN
 
Top