1 Button 2 Functions

Discussion specific to the 24-pin ZX microcontrollers, e.g. ZX-24r, ZX-24s and ZX-24t.
Post Reply
pcleats
Posts: 35
Joined: 12 December 2005, 11:57 AM

1 Button 2 Functions

Post by pcleats »

Hello all,

What I will need to be able to do is use a single push button to preform 2 seperate functions. If I push the button for less than a second then it turns the power to the unit on. If I hold the same button for more then 3 seconds then it will enable a seperate function.

I know there are multiple ways to do this. I am looking for suggestions on the most efficient way.

The code below is what I am currently using to check the switch to turn the power on.

Code: Select all

Private PowerButtonTaskStack(1 to 120) as Byte

CallTask PowerButtonTask, PowerButtonTaskStack

'------------------------------------------------------------- 
'Check to see if the start button has been pressed
'
'
Sub PowerButtonTask() 
   Do 
      ' wait for a button press 
      Call WaitForInterrupt(zxPinFallingEdge, 0) 

      ' toggle the device state 
      Call PutPin(RelayPin, IIf(deviceIsOn, Power_off, Power_On)) 
	deviceIsOn = Not deviceIsOn
	If deviceIsOn = False then
		Call LCD_Init
	end if

      ' Time delay for switch debounce 
       Call Delay(0.400) '400mS time delay
   Loop 
End Sub
As always Thanks

Patrick
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Post by spamiam »

Well, the simplest technique would be:


1) Wait for interrupt indicating a button press

2) check the RTC timer and save the value

3) delay a debounce period

4) Wait for interrupt indicating button release

5) Check the RTC timer and subtract the last value. This is how long it was depressed.

6) You might delay a debounce period after this



Another technique might be like this:


1)check to see if button is depressed.

2) wait 1 second and check button state.

3) If new state is NOT depressed, then what? Not applicable if less than 1 sec?

4) if it is still depressed then wait for 2 more seconds and check button state.

5) If it is now NOT depressed then it was a "short" press.

6) If is is still depressed, then it is a "Long" press.

I would not bother to use a wait for interrupt function for the button state checking, but I might use it to wait for the initial button press. I would use a delay or sleep functon to get the waits within the discrimination function.

You may want more discrimination:

if press < 1 second = nothing happens

if 1<press<2 then it is "short"

if 2<press<3 then nothing

if press>3 then "long"

To do this you would just do some checking at the pertinent points in time. Debouncing may not be necessary because if you are in the middle of a bounce cascade and get a spuriously "depressed" reading when the user has JUST released the button, then they did it JUST a little too late and it counts as a longer button press.

With this technique, you can not be sure there were not MULTIPLE press and releases within the 3 second window. But if the user wants to go crazy on the button, then that is just too bad for them.
pcleats
Posts: 35
Joined: 12 December 2005, 11:57 AM

Post by pcleats »

As it turns out it is easier than I thought to make this work. I have a real bad habit of making things harder than they need to be.

Anyway thanks for the suggestions.

Patrick
Post Reply