1 switch, 1 pin, "single click, double click..."


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink

    Hi, Steve

    Might work with two " Button" commands following each other, as debounce time is effective ONLY if condition TRUE ...

    he,he ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Maybe... but as you know, I never use Button, so...

    Someone could also go crazy and use CLKI + timer interrupt.... this allow 1,2,3,4,5... click triggers for a x period of time, with a real minimum of code line, something <10 code line I guess.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post

    Quote Originally Posted by Acetronics View Post
    Hi, Steve

    Might work with two " Button" commands following each other, as debounce time is effective ONLY if condition TRUE ...

    he,he ...

    Alain
    I haven't use Button -command too, but couple of IF - THEN with PAUSE works for me.
    Code:
    IF But = 0 then         'Button is pressed and debounce detections starts 
        PAUSE 100           'wait 100ms or some other more suitable time
        IF But = 0 then     'if button is still held down then do something
                            'start timer or loop for 2nd button press detection ...
    I think, some time window must be defined otherwise you will be waiting that other press forever...

    Also nice to hear that Steve had similar thoughts

    BR,
    -Gusse-
    Last edited by Gusse; - 28th March 2009 at 11:04.

  4. #4
    Join Date
    Jul 2007
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    thanks for all the input guys.

    I played around with the count for a little while, thought it was working pretty consistently. I re-evaluated where the button was going to be positioned and thought the double clicking could become annoying.

    The if-then statements turned out to be very nice change, and 10 fold more consistent.

  5. #5


    Did you find this post helpful? Yes | No

    Default

    I did this a while ago. I just removed the "bells and whistles" and should work the way it is. It works like a single or double click on a mouse.

    START:
    IF PORTA.1 = 1 Then START 'BUTTON NOT PUSHED STAY HERE
    GoSub MOUSECLICK 'CHECK FOR DOUBLE CLICK
    if click = 1 then let click = 0 : goto something
    if click = 2 then let click = 0 : goto somethingelse
    goto start

    MOUSECLICK:
    IF PORTA.1 = 0 Then MOUSECLICK 'stay here until switch is released
    LET CLICK = CLICK + 1 'switch released
    IF CLICK >=2 Then Return 'LIMIT CLICKS TO 2
    pause 500 'switch has to be pushed 2 times within 1/2 second for double click
    IF PORTA.1 = 0 Then MOUSECLICK 'switch pushed again
    Return

  6. #6
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post

    Quote Originally Posted by peterdeco1 View Post
    I did this a while ago. I just removed the "bells and whistles" and should work the way it is. It works like a single or double click on a mouse.

    START:
    IF PORTA.1 = 1 Then START 'BUTTON NOT PUSHED STAY HERE
    GoSub MOUSECLICK 'CHECK FOR DOUBLE CLICK
    if click = 1 then let click = 0 : goto something
    if click = 2 then let click = 0 : goto somethingelse
    goto start

    MOUSECLICK:
    IF PORTA.1 = 0 Then MOUSECLICK 'stay here until switch is released
    LET CLICK = CLICK + 1 'switch released
    IF CLICK >=2 Then Return 'LIMIT CLICKS TO 2
    pause 500 'switch has to be pushed 2 times within 1/2 second for double click
    IF PORTA.1 = 0 Then MOUSECLICK 'switch pushed again
    Return
    As you can see from picture, debounce can make port to change state between 0 - 1 for several times. This would make code above to generate two clicks if pressed longer than 500ms (even button is pressed once). More reliable way is to check that button is still held down after some time (debounce delay) and after that go forward.

    Source of picture and some more info about debounce
    (I hope it is OK to link this picture to this forum? If not then Admin can remove it.)

    Problem is here
    Code:
    MOUSECLICK:
    IF PORTA.1 = 0 Then MOUSECLICK 'stay here until switch is released
    Click (similar to picture above): If shorter than 500ms then OK, but longer pulses than 500ms will generate 2 clicks. You have no debounce filtering in this loop.
    Of cource, some can say that this is very unlikely scenario. Maybe, but it can happen if you don't have anykind of filter (i.e RC-loop) at input.

    BR,
    Gusse

  7. #7


    Did you find this post helpful? Yes | No

    Default

    You are right Gusse. When I removed all the "bells and whistles" , I removed the debouncing and also a timer where "pause 500" is. If you double click before the 500 mS is up, it immediately makes click = 2 and returns.

  8. #8
    Join Date
    Apr 2009
    Location
    Northport Alabama USA
    Posts
    6


    Did you find this post helpful? Yes | No

    Default

    What is challenging is doing this without making the rest of the program be suspended waiting on the second click to happen or not.

    The way I have been doing this is to set a bit for each of the following events:

    Button pressed
    Time expired without second press
    Second press happened during time count.

    The following sample works. It may look complicated, but it works well. It is part of my car lights controller code which has multiple inputs that have similar logic and all are active at once.

    While one input is waiting on a possible second press, the rest of the program is still working and responding to inputs.

    I've always found that for best reliability a pushbutton almost always need a small capacitor across the contacts to debounce it. Using the following program, the loop is so short the program can respond very fast. Without a capacitor, the bounce can cause it to give false operation. On a longer program, the remainder of the program loop will be enough delay to debounce the button signal.

    The complete lights controller code has over 800 lines of code, and a good portion has to execute every program cycle. Each input is checked once per cycle so there is enough delay to keep bounce from being an issue.

    Also, read the state of the input into a temp variable, then use the temp variable to do the logic. This will keep bounce from happening during the logic operations, and causing some really funky stuff to happen! Voice of experience!!

    I used the hardware of the lights controller to test this program fragment, so that's why the relays are on it.

    Hope this is helpful!
    Thanks,
    David

    Code:
    'Double-Click example For PIC16F88
    
    'Header for 16F88 CPU I/O modes and Osc. speed...
    CMCON=7 'Analog comparators OFF
    ANSEL=0 'ADC inputs OFF
    'OSCCON=%00000010 ' 31.25 KHz
    'OSCCON=%00010010 ' 125 KHz
    'OSCCON=%00100010 ' 250 KHz
    'OSCCON=%00110010 ' 500 KHz
    'OSCCON=%01000010 ' 1 MHz
    'OSCCON=%01010010 ' 2 MHz
    'OSCCON=%01100010 ' 4 MHz
    OSCCON=%01110010 ' 8 MHz
    
    SetupSwitch VAR PORTA.5 'This is a momentary pushbutton
    Relay1 VAR PORTB.2      'These outputs close a relay when they are HIGH
    Relay2 VAR PORTB.1
    
    SStime VAR WORD         'this counts down to time the second press
    
    x VAR BIT               'buffer variable to keep switch state changes during logic from causing issues.
    SSx VAR BIT             'determines change of switch
    SS1 VAR BIT             'Set once when SetupSwitch pressed once
    SS2 VAR BIT             'Set once when SetupSwitch pressed twice within timefrave
    
    SStime = 0 'These must be initialized to keep from having one false-trigger when circuit sterts up.
    SSx = 0
    
    :loop
    
    '------
    X = SetupSwitch 
    IF X <> SSx Then
     PauseUs 1 'This will help de-bounce switch, but if program is long, you will not need!
     LET SSx = X
       IF X Then
        IF SStime > 0 Then 'If the timer is still counting when a press happens
         SS2 = 1           'set the "double press" variable and
         SStime = 0        'reset the timer
        Else
         SStime = 10000    'When the first press happens, start counting at 10000.
        EndIF
       EndIF
    Else
     SS1 = 0
     SS2 = 0  
    EndIF
    
    IF SStime = 1 Then 'If no second press has occured, when timer reaches 1, set the "single press" bit.
     SS1 = 1
     SStime = 0
    EndIF
    '------
    
    IF SStime > 0 Then        'This function to decrement the double-click timer would be done in another
     LET SStime = SStime - 1  'part of the program, probably using a hardware timer. This is just an example. 
    EndIF
    
    IF SS1 Then Toggle Relay1 'Each single-click toggles Relay # 1
    IF SS2 Then Toggle Relay2 'Each double-click toggles Relay # 2
    
    GoTo loop

Similar Threads

  1. RS485 bus - starting probem
    By wurm in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th January 2010, 14:35
  2. Optimizing DIV
    By skimask in forum mel PIC BASIC Pro
    Replies: 41
    Last Post: - 22nd September 2008, 05:58
  3. Keypad unlock (as in garage door possibly)
    By Fred in forum Code Examples
    Replies: 5
    Last Post: - 2nd April 2006, 05:26
  4. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 23:43
  5. DS1820 headache
    By Calco in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 12th August 2004, 01:28

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts