How to define a new function when a button is pushed long. Can it be done?

tschrama

New Member
hi all,

On a 08M2 chip, I am searching for a way to make a button do something entirely different when that button is being hold for more than 1 sec. I want it to write something to the EEPROM, like a 'store' function.

When being pushed and released shortly it should carry out it's normal function (switch the next relay).

Can it be done?

Greetz,
Thijs
 

hippy

Technical Support
Staff member
Welcome to the PICAXE forum.

You can probably do it the hard way by checking if the button is held and for how long, something like this -

Code:
Symbol BTN         = pinC.3
Symbol PUSHED      = 1

Symbol heldTimeout = w0

Symbol PAUSE_TIME  = 10                ; 10ms
Symbol TIMEOUT     = 1000 / PAUSE_TIME ; 1000ms / 1s

Do
  If BTN Is PUSHED Then
    If heldTimeout > 0 Then
      Pause PAUSE_TIME
      heldTimeout = heldTimeout - 1
      If heldTimeout = 0 Then
        Gosub HeldForTimeoutPeriod
      End If
    End If
  Else
    heldTimeout = TIMEOUT
  End If
Loop
Or you could do all that by using the BUTTON command -

http://www.picaxe.com/BASIC-Commands/Digital-InputOutput/button
 

tschrama

New Member
Welcome to the PICAXE forum.

You can probably do it the hard way by checking if the button is held and for how long, something like this -

Code:
Symbol BTN         = pinC.3
Symbol PUSHED      = 1

Symbol heldTimeout = w0

Symbol PAUSE_TIME  = 10                ; 10ms
Symbol TIMEOUT     = 1000 / PAUSE_TIME ; 1000ms / 1s

Do
  If BTN Is PUSHED Then
    If heldTimeout > 0 Then
      Pause PAUSE_TIME
      heldTimeout = heldTimeout - 1
      If heldTimeout = 0 Then
        Gosub HeldForTimeoutPeriod
      End If
    End If
  Else
    heldTimeout = TIMEOUT
  End If
Loop
Or you could do all that by using the BUTTON command -

http://www.picaxe.com/BASIC-Commands/Digital-InputOutput/button

Thx for the welcome! :D

I did try to figure out how to do it with the 'button'command, but I cant see how I can do that. It seems the button command only enables to repeat a action. Going to study your code snippet now.. thx!
 

tschrama

New Member
Well I tried but I couldn't find an elegant way, I doubt mine is any better then your example. Any remarks are highly valued.

Code:
butn:		if pinC.3 = 1 then		'button is released
			if b3 = 20 then		'pressed > 1000ms
			    	write b2, b1  	'store programm in EEPROM
			else				'pressed < 1000ms
				b3 = b3+1		'increment pressed counter
			endif
		endif
		
		if pinC.3 = 0 then		'button is released
			if b3>0 then 		'pressed > 50ms
				if b3<20 then	'pressed < 1000ms
					b0 = b0+1	'increment preamp channel
					if b0>3 then	'if b0 overflows to 4
						b0=0		'set it to 0
					endif
					gosub setpreamp	'set the preamp relays
				endif
				b3 = 0		'reset pressed counter to 0
			endif
		endif
		return
 

reywas

Member
Here's how I do it:

Code:
[color=Blue]if button [/color][color=DarkCyan]= [/color][color=Black]pushed [/color][color=Blue]then                         [/color][color=Green];button pushed?                           
      [/color][color=Blue]pause [/color][color=Navy]10                                  [/color][color=Green];10mS debounce
      [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]0                                    [/color][color=Green];clear "time accumulator"
      [/color][color=Blue]do while button [/color][color=DarkCyan]= [/color][color=Black]pushed [/color][color=DarkCyan]and [/color][color=Purple]b1 [/color][color=DarkCyan]< [/color][color=Navy]10      [/color][color=Green];button still pushed and hold time < 1S   
            [/color][color=Blue]inc [/color][color=Purple]b1
            [/color][color=Blue]pause [/color][color=Navy]100                           [/color][color=Green];100mS
      [/color][color=Blue]loop
      
      if [/color][color=Purple]b1 [/color][color=DarkCyan]< [/color][color=Navy]10 [/color][color=Blue]then                           [/color][color=Green];if button not held for 1 sec
            ;code
      [/color][color=Blue]else                                      [/color][color=Green];button pushed for 1 sec
            ;code
      [/color][color=Blue]endif
endif[/color]
 
Last edited:

wapo54001

Senior Member
I don't use the button command, I do this by resetting the timer to zero when the button is pushed, then measuring the timer when the button is released to determine which action to take. On a rotary encoder, I use the shaft press in three different ways -- immediate press and release, a one-to-three second press, and a greater than three second press. If you have a display, you could compress the timing a bit and simply release when the correct option displays on the screen or LCD display.
 
Top