Searching \ for 'Single switch, two functions' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=single+switch+two
Search entire site for: 'Single switch, two functions'.

Truncated match.
PICList Thread
'Single switch, two functions'
1999\07\08@015905 by Bob Wake & RenŽe McMeeken

flavicon
face
Any idea where to start writing code to enable a single switch to
perform two functions? I've seen switches that do one thing if pressed
briefly and another if held down for 1 sec. or more.
Can this be achieved only with a timer interrupt, or is there a better
way?

1999\07\08@024544 by Peter McAlpine

flavicon
face
Hi,
If your write the debounce code to work out in which
direction the key is going, i.e. key_down or key_up,
Then when a key_down is received start a timer and
if after a preset time a key_up has not cancelled it then
do a key_held operation.

I have some C code for a non-PIC micro if you are
interested.

Regards
Peter Mcalpine
spam_OUTpetermcaTakeThisOuTspamozemail.com.au - http://www.ozemail.com.au/~petermca
.....mcalpineKILLspamspam@spam@gme.net.au - http://www.gme.net.au
http://www.wrx.org.au

----- Original Message -----
From: Bob Wake & RenŽe McMeeken <rwakespamKILLspamPATHCOM.COM>
To: <.....PICLISTKILLspamspam.....MITVMA.MIT.EDU>
Sent: Thursday, 8 July 1999 3:57
Subject: Single switch, two functions


> Any idea where to start writing code to enable a single switch to
> perform two functions? I've seen switches that do one thing if pressed
> briefly and another if held down for 1 sec. or more.
>  Can this be achieved only with a timer interrupt, or is there a better
> way?

1999\07\08@063929 by Caisson

flavicon
face
> Van: Bob Wake & RenŽe McMeeken <EraseMErwakespam_OUTspamTakeThisOuTPATHCOM.COM>
> Aan: PICLISTspamspam_OUTMITVMA.MIT.EDU
> Onderwerp: Single switch, two functions
> Datum: donderdag 8 juli 1999 7:57

Hello Bob & RenŽe

>  Any idea where to start writing code to enable a single switch to
> perform two functions? I've seen switches that do one thing if pressed
> briefly and another if held down for 1 sec. or more.
>  Can this be achieved only with a timer interrupt, or is there a better
> way?

Better ?  What's better ...  Other ?  Sure !  :-)

Clear counter
while key is down
 advance counter
wend
if counter-value is _really_ low
 exit   ;Key is bouncing !
endif
if counter-value is Low-ish
 Do-something
else   ;counter-value is high-ish
 Do-something else :-)
endif

Greetz,
 Rudy Wieser

1999\07\08@103724 by Harold Hallikainen

picon face
       Could be done with a couple debounce loops.  I did something similar
with single and double clicks on the same button (next time you're at the
dentist, look for the Virtuoso device for curing composites used to fill
teeth).

Harold

On Thu, 8 Jul 1999 01:57:53 -0400 Bob Wake & =?iso-8859-1?Q?Ren=E9e?=
McMeeken <@spam@rwakeKILLspamspamPATHCOM.COM> writes:
> Any idea where to start writing code to enable a single switch to
>perform two functions? I've seen switches that do one thing if
>pressed
>briefly and another if held down for 1 sec. or more.
> Can this be achieved only with a timer interrupt, or is there a
>better
>way?

___________________________________________________________________
Get the Internet just the way you want it.
Free software, free e-mail, and free Internet access for a month!
Try Juno Web: dl.http://www.juno.com/dynoget/tagj.

1999\07\08@123955 by Dan Creagan

flavicon
face
I'm new at PICs but not new to coding. If I was attempting this, I would
start with a direct solution and then tweak it:

1. Detect switch pressed
2. Pause 400 ms to debounce (assuming Radio Shack tuning fork)
3. Pause 600 ms and periodically check for switch release. If released, it
is a 'short press' switch so return.
4. On return, check switch. If it is still closed, then process 'long press'
request, else process 'short press' request.

This could be done in the main program or as an interrupt. If your program
is just ticking over waiting for the switch, then it doesn't need the
interrupt. If you are doing magnificent things with the chip while you are
waiting for the switch, then set the switch on an external interrupt.

If you need help with code for this, I think I could send a short .asm
example. If you were looking for something other than what I answered, then
please ignore all that preceded.

Later,

Dan

Simple CCS code examples at: http://204.233.101.40/robots/pic1.html


{Quote hidden}

1999\07\08@153024 by w. v. ooijen / f. hanneman

picon face
My 'PIC assignments' contain one 'lesson'
that explains how to make a code slot
with one button.
http://www.xs4all.nl/~wf/wouter/pic/djo/e_index.html

----------
{Quote hidden}

1999\07\08@185620 by paulb

flavicon
face
Dan Creagan wrote:

> This could be done in the main program or as an interrupt.  If your
> program is just ticking over waiting for the switch, then it doesn't
> need the interrupt.  If you are doing magnificent things with the chip
> while you are waiting for the switch, then set the switch on an
> external interrupt.

 Just on a philosophical note, I see it the other way round!  Do by all
means put the switch on an interrupt line - unless the interrupt is
needed by something important!  However, if the program is "just ticking
over waiting for the switch" it could just as easily be sleeping.  And
you don't even need an interrupt for that as it could be periodically
waking by the watchdog and polling the switch.

 If OTOH "you are doing magnificent things with the chip" in the
meantime, then you really don't need an interrupt for the switch and in
fact, an interrupt would be more of an embarrassment as it will occur
while the "magnificent things" are in mid-stride.

 At the speed of a microprocessor, a human-interfaced switch is a
trivial and imprecise matter.  The loop (there's always a loop!) running
your magnificent task will in the vast majority of cases have a
predictable duration within 50% and that will be a sufficient timebase
for your debounce and long/ short discrimination.

 As well, many applications will have a timer "tick" generated by the
TMR0/ prescaler *either* polled or under interrupt, and this routine can
poll the switch.

 I think this is significant as while the program *might* do nothing
more than wait for a switch and activate an output, there is invariably
something else useful it might do in the meantime ("software creep").

 Also, just because the chip *provides* interrupts does not make them
appropriate in any application (especially in PICs!).
--
 Cheers,
       Paul B.

1999\07\08@211522 by Mike Keitz

picon face
On Thu, 8 Jul 1999 16:46:40 +1000 Peter McAlpine <TakeThisOuTmcalpineEraseMEspamspam_OUTGME.NET.AU>
writes:

>Then when a key_down is received start a timer and
>if after a preset time a key_up has not cancelled it then
>do a key_held operation.

I did it by polling the key at a constant rate (a good idea in any case)
and using a counter in RAM.   After each poll, if the key is not pressed,
the poll counter gets reset to its starting value.  If the  key is
pressed, the counter counts down by 1, unless it is already zero.  When
the count reaches zero, the "key held" action is performed.

What is not so obvious at first is that holding a button down requires
first pressing it.  In some cases you need to be careful not to falsely
do the "key pressed" action if the user actually is holding the key down.
If the "key held" action is not a direct extension of the "key pressed"
action, it is necessary to not do the "key pressed" action until the key
is released (with the count not zero).  They haven't made a
microcontroller that can predict the future yet.

If your polling rate is slower than the maximum contact bounce time (50
ms is usually a good choice), it isn't necessary to do any explicit
debouncing.  If you are polling fast enough that bouncing is a problem,
the "key pressed" logic could be extended a little to use the counter for
debouncing too.  It would look for a count which is several counts less
than the maximum, but not zero.  That would mean that the key had been
detected pressed for enough polls to count as really pressed, but not
held down.


___________________________________________________________________
Get the Internet just the way you want it.
Free software, free e-mail, and free Internet access for a month!
Try Juno Web: dl.http://www.juno.com/dynoget/tagj.

More... (looser matching)
- Last day of these posts
- In 1999 , 2000 only
- Today
- New search...