Button/switch debouncing


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    I think in the simplest form it's overkill...

    Look, you're not trying to ignore static from a localised lightning strike, just trying to detect that a Button has been pressed... so simply...
    Code:
    	If UserButton=0 then
    		Pause 10
    		Goto ButtonPressed
    		endif
    The IF statement checks if your Button has been pressed... The PAUSE simply ensures that you don't check for the press again within a set time period (which should be longer than your worst-case anticipated debounce time). Now, for user entry, I like to have a 100mS pause, this gives a nice 10cps repitition rate if you keep your finger on the button. If you only want ONE keypress, then simply check that it's been released first before doing anything else...
    Code:
    	While UserButton=0:Wend
    If you have a scenario where you have problematic static, then check for a Button Press once, pause a while, and check it's still pressed some time later. Static is momentary, so you will eradicate an awful lot of false triggering with something like...
    Code:
    	If UserButton=0 then
    		Pause 10
    		If UserButton=0 then Goto ButtonPressed
    		endif

  2. #2
    Join Date
    Jul 2008
    Posts
    3


    Did you find this post helpful? Yes | No

    Default coding for button by using PIC18F4620

    Hi everyone..

    I'm new in this forum and I hope u guys can help me to solve my problem. I'm try to do a coding for button by using PICF4620 but there is an error.I also not sure whether my program is right or not because this is the first time i'm using Pbasic.

    Here the program:

    define osc 25

    define INIT_ADCON2=0
    define INIT_TRISA.2=0

    DEFINE INIT_ADCON0=1
    DEFINE INIT_TRISB.0=1

    DEFINE INIT_ADCON1=1
    DEFINE INIT_TRISB.1=1

    LED VAR porta.2

    BUTTON0 VAR portb.0
    BUTTON1 var portb.1

    main:

    high BUTTON0=1
    high LED ;led is ON
    pause 600

    low LED ;led is OFF
    pause 600

    high BUTTON1=1
    high LED ;led is ON
    pause 600

    low LED ;led is OFF
    pause 600


    goto main

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    This is INCORRECT...

    high BUTTON0=1

    You've aliased BUTTON0 as a pin on your pic... now are you using it as an INPUT in which case you need an IF statement, or are you using it as an OUTPUT in which case you can use the HIGH statement...

    Let's assume you're using it as an INPUT and checking is it has been pressed... Buttons are usually pulled LOW when pressed because they are connected between the PIC pin and 0v. There is usually a pull-up Resistor on the pin, but since you are using PORTB then you will find you can probably enable internal pull-up's to do that job... so in this case your statement would be...

    If BUTTON0=0 then
    HIGH LED
    Pause 600
    endif

    Also, your DEFINE statements look awfully strange to me if you are using MeLabs PICBasic (which this forum is intended for)... if you are using anyone elses BASIC product, you might not get much help here...

  4. #4
    xnihilo's Avatar
    xnihilo Guest


    Did you find this post helpful? Yes | No

    Default

    Melanie, Darrel and Rob, thank you for your answers.
    It helps.

    Regards.

  5. #5
    Join Date
    Jul 2008
    Posts
    3


    Did you find this post helpful? Yes | No

    Smile

    I forgot to mention that i use the button for INPUT..anyway thank you Melanie.
    It helps.

Similar Threads

  1. What debouncing technicques do you know?
    By Humbleworker in forum General
    Replies: 4
    Last Post: - 30th March 2007, 16:55
  2. Debouncing
    By Demon in forum Documentation
    Replies: 0
    Last Post: - 24th October 2005, 19:40

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