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


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1


    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.

  2. #2
    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