Button press and press & hold how to ?


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2007
    Posts
    53

    Default Button press and press & hold how to ?

    I would like to know what is the best way to code a program to call two different subroutines depending if I either press OR "press and hold" a button.

    I want to call "STOP" subroutine evey time a button is pressed, and I also want to call "PARK" subroutine if the button is hold for a few seconds.


    Any suggestion?

    J-P

  2. #2
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Hello grandpa,

    Maybe this can be good for you.

    Code:
    Button VAR PORTB.0 ' Your Input switch
    Loop    VAR WORD
    
    InputButton:
    IF Button = 1 THEN Check4Button
    GOTO InputButton
    
    Check4Button:
    FOR Loop = 0 TO 1000 ' 1 Second
         PAUSE 1
         IF Button = 0 THEN Stop ' If button was released before 1 Sec = short keypress
    NEXT Loop
    IF Button = 1 THEN Park ' If button was hold longer than 1 Sec = long keypress
    GOTO InputButton
    
    Stop:
    ' Here is your code
    
    Park:
    ' Here is your other code
    
    END
    regards Rob

  3. #3
    Join Date
    Jun 2007
    Posts
    56


    Did you find this post helpful? Yes | No

    Default

    Hi Grandpa,

    As you use push switch, don't forget to debounce it

    Johan

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Robson View Post
    Hello grandpa,

    Maybe this can be good for you.

    Code:
    Button VAR PORTB.0 ' Your Input switch
    Loop    VAR WORD
    
    InputButton:
    IF Button = 1 THEN Check4Button
    GOTO InputButton
    
    Check4Button:
    FOR Loop = 0 TO 1000 ' 1 Second
         PAUSE 1
         IF Button = 0 THEN Stop ' If button was released before 1 Sec = short keypress
    NEXT Loop
    IF Button = 1 THEN Park ' If button was hold longer than 1 Sec = long keypress
    GOTO InputButton
    
    Stop:
    ' Here is your code
    
    Park:
    ' Here is your other code
    
    END
    regards Rob


    Something to pay attention for:


    Button and Stop are reserved words =>> they are commands.

    You may want to rename them as Btn and Stp.

    ============================
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Oh of course. How I forget it ;-x
    Maybe it was too late 4 me ...
    Code:
    Btn VAR PORTB.0 ' Your Input switch
    LoopTime VAR WORD
    
    InputButton:
    PAUSE 20 ' Button delay or use DEFINE BUTTON_PAUSE before
    IF Btn = 1 THEN Check4Button
    GOTO InputButton
    
    Check4Button:
    FOR LoopTime = 0 TO 1000 ' 1 Second
        PAUSE 1
        IF Btn = 0 THEN Stp ' If button was released before 1 Sec = short keypress
    NEXT LoopTime
    IF Btn = 1 THEN Park ' If button was hold longer than 1 Sec = long keypress
    GOTO InputButton
    
    Stp:
    ' Here is your code
    
    Park:
    ' Here is your other code
    
    END
    Last edited by Robson; - 21st August 2007 at 08:47.

  6. #6
    Join Date
    Jul 2007
    Location
    Grass Valley CA
    Posts
    16


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Robson View Post

    Check4Button:
    FOR Loop = 0 TO 1000 ' 1 Second
    PAUSE 1
    IF Button = 0 THEN Stop ' If button was released before 1 Sec = short keypress
    NEXT Loop
    IF Button = 1 THEN Park ' If button was hold longer than 1 Sec = long keypress
    GOTO InputButton

    Stop:
    ' Here is your code

    Park:
    ' Here is your other code

    END
    [/code]

    regards Rob

    I'm a real newbie at the PIC, but my BASIC is fairly decent. What is the reason for the GOTO InputButton command at the end of the FOR-NEXT loop? The button will be either zero (in which case you GOTO Stp) or one (in which case you GOTO Prk). How would the code ever get to that GOTO InputButton command? Or is that a belt-and-suspenders command on the off chance that the button is released during the last millisecond of the loop?

    Somebody said something about switch debounce. Is there a standard debounce code snippet for pushbutton switches? A 50 millisecond delay waiting for the sucker to stop clanging around?

    Jim

  7. #7


    Did you find this post helpful? Yes | No

    Default

    Hello Everyone. I use a similar system on a voice record circuit. The record button works as a record button but if pushed and held without making a recording, it resets the voice record chip to a preset address at the beginning of memory. Snippets of code below. Perhaps you could use this system for your circuit.

    'PORTB.0 IS THE RECORD BUTTON, 0 = PUSHED, 1 = RELEASED
    'PORTB.1 IS MESSAGE BUTTON, 0 = PUSHED, 1 = RELEASED

    IF PORTB.0 = 0 AND PORTB.1 = 0 THEN RECORD 'RECORD MESSAGE
    IF PORTB.0 = 0 AND PORTB.1 = 1 THEN RESETMESSAGES 'RECORD BUTTON PUSHED ALONE

    RECORD:
    DOES THE RECORD FUNCTION

    RESETMESSAGES:
    IF PORTB.0 = 0 AND PORTB.1 = 0 THEN RECORD 'CHECK IF BOTH ARE PUSHED BEFORE 3 SECONDS
    LET RESETTIMER = RESETTIMER + 1
    Pause 100
    IF RESETTIMER >= 30 Then MAKESOUND 'MAKES BEEP AFTER 3 SECONDS AND CONTINUES PROGRAM
    IF PORTB.0 = 0 THEN RESETMESSAGES
    IF PORTB.0 = 1 THEN START 'BUTTON RELEASED GOTO START

  8. #8
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by weirdjim View Post
    I'm a real newbie at the PIC, but my BASIC is fairly decent. What is the reason for the GOTO InputButton command at the end of the FOR-NEXT loop? The button will be either zero (in which case you GOTO Stp) or one (in which case you GOTO Prk). How would the code ever get to that GOTO InputButton command? Or is that a belt-and-suspenders command on the off chance that the button is released during the last millisecond of the loop?

    Somebody said something about switch debounce. Is there a standard debounce code snippet for pushbutton switches? A 50 millisecond delay waiting for the sucker to stop clanging around?

    Jim
    1. GOTO InputButton is for start at the beginning after the button was detected as pressed. Going to Loop the program
    2. If the button was released before the timer ends at 1 second means -> short press of the button.
    3. ELSE long press of button.
    4. Insert a pause of 20ms -50ms. My best results measured with 20ms for Button debounce.
    If you have a complex matrix of columns and rows with double action feature like this, your code will slow down. I also prefer to use for next loops with Pause 1, while time critical projects and Interrupt routines.
    If you insert
    PAUSE 2000 is it impossible to jump to the Interrupt routine, when your pause is active.

    Delay2: ' Now it´s possible to interrupt during the PAUSE and jump to your Interrupt location
    FOR x = 0 TO 1000
    PAUSE 1
    NEXT x
    RETURN

    I mean that are my techniques, everyone should code like he likes it.

  9. #9
    Join Date
    Jul 2007
    Posts
    53


    Did you find this post helpful? Yes | No

    Smile I'm learning something here

    Hi Robson,

    you said: "PAUSE 20 ' Button delay or use DEFINE BUTTON_PAUSE before"

    I tought before that DEFINE BUTTON_PAUSE was used only by the BUTTON command. Now, from what you wrote is that correct to say that BUTTON_PAUSE will put a debounce delay on all PIC inputs pins?

    J-P

  10. #10
    Join Date
    Jun 2007
    Posts
    56


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by weirdjim View Post
    Somebody said something about switch debounce. Is there a standard debounce code snippet for pushbutton switches? A 50 millisecond delay waiting for the sucker to stop clanging around?

    Jim

    You can use software debounce ( with Button command ) , but if you dont want to change the code, just put the switch in parallel with a resitor and capacitor, something like this:
    Code:
               sw
        -------_-------
    ---!               !------
        --vvv----||----! 
           R        C
    Last edited by Johan; - 22nd August 2007 at 03:39.

Similar Threads

  1. 4 Bytes one button press
    By Dennis in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th January 2010, 22:36
  2. Giving power to MCU upon button press - Good Idea?
    By financecatalyst in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 21st July 2009, 07:34
  3. multi functions button press
    By malwww in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 27th May 2009, 00:12
  4. Using Sleep
    By elec_mech in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th August 2008, 04:05
  5. Sleep until button press?
    By kevj in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th October 2007, 03:47

Members who have read this thread : 2

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