Better Pushbutton


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    22

    Default Better Pushbutton

    I'm using a very good quality normally open switch to change HPWM for an array of LEDs. This switch has some sort of compound action where the contacts snap closed when the button is pressed. A lot of times I press the switch and nothing happens. I've played with the pause time in the code and changed it to different settings from 1 ms to 100 ms, but it doesn't seem to make much difference. Is there are more reliable code for a switch like this? Here's the code:

    ~ Dave

    Code:
    @ device pic16F628A, INTRC_OSC_NOCLKOUT, wdt_off, mclr_off, lvp_off, protect_off
    
    CMCON = 7
    CounterA Var Byte
    CounterA=0
    
    start:
    
        HPWM 1,64,2000 ' hardware pulse width modulate channel 1, 25% duty, 2khz
        goto Main
    
    Main:
        If PORTA.1=0 then 
            If CounterA<=1 then        
                CounterA=CounterA+1
                else 
                   CounterA=0
                endif
            endif
        While PORTA.1=0 : Wend : pause 10
        BRANCHL CounterA,[LabelOne,LabelTwo]
        
        Goto Main
    
    LabelOne:
        HPWM 1,64,2000 ' hardware pulse width modulate channel 1, 25% duty, 2khz
        goto Main
    
    LabelTwo:
        HPWM 1,254,2000 ' hardware pulse width modulate channel 1, 99.6% duty, 2khz
        goto Main

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


    Did you find this post helpful? Yes | No

    Default

    What pull-up resistor value do you have?

    Just for fun try this one
    Code:
            CMCON = 7
            DutySelect var bit
            Dutyselect = 0
            gosub ChangeDuty
                               
    Main:
            If PORTA.1=0 then
                   pause 20
                   dutyselect=dutyselect ^1
                   gosub ChangeDuty
                   while porta.1=0 : Wend
                   Pause 20                      
                   endif
    
            goto main                  
            
    ChangeDuty:
            If DutySelect then
                hpwm 1,254,2000
                else
                    hpwm 1,64,2000
                endif
                
            return
    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

    Have you check contact resistance of the switch when it is pressed? What kind of contact surfaces the switch has?

    With too strong pull-ups you might get this kind of problems if switch is unreliable (leave on some high ohmic state even when pressed).

    BR,
    -Gusse-

  4. #4
    Join Date
    Jan 2009
    Posts
    22


    Did you find this post helpful? Yes | No

    Default

    I'm using 10k pullups. How high should I go?

    Contact resistance is 0.046 ohms and the switch is a very high quality, high reliability switch. The contacts are either closed or not.

    OK, I'll try mister_e's version.

    Thanks!

    ~ Dave

  5. #5
    Join Date
    Jan 2009
    Posts
    22


    Did you find this post helpful? Yes | No

    Default

    OK, mister_e's program worked MUCH better .... why is that?

    Thank you once again!

    ~ Dave

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


    Did you find this post helpful? Yes | No

    Default

    Mine don't refresh the PWM duty cycle all the time, it will do IF a push button is pressed.

    The main difference is how the switch debounce is made. Let's use a Google Image

    This is what the signal looks like when you press your switch, the same noise should also appear on the rising edge when you release the switch.

    A little pause after you press on the switch is probably not necessary because your code need some time to execute, but 20mSec for safety sake that's nothing huh?

    If you had more code after the switch detection, you could reduce the second PAUSE.

    HTH
    Steve

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

Similar Threads

  1. USBDemo, something to learn USB a little bit
    By mister_e in forum Code Examples
    Replies: 278
    Last Post: - 1st May 2014, 00:38
  2. Make pushbutton "1" when pressed?
    By Yodoad in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th May 2009, 22:04
  3. one button-two operations
    By savnik in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 20th July 2006, 02:24
  4. Timing the pressing and releasing of a PushButton ?
    By serandre in forum Code Examples
    Replies: 4
    Last Post: - 30th March 2006, 12:43
  5. Pushbutton code routine suggestions?
    By jessey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd September 2005, 01:02

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