will this work for dedecting a held button?


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189

    Default will this work for dedecting a held button?

    What I want to do is have the user press a button and get a "beep" to acknowledge the button was pressed, but if the button is held for a short period of time the program will assume something else is to be done and jump to that routine.

    The initial button command, no problem. The "held" status of the button puzzled me a bit, but I have came up with this idea.

    If gives the user a short time to leave his finger on the button then if it doesn't detect it the program assumes the button wasn't held and starts again.

    I have to do this for four buttons so I don't think it is the most efficient way, but I think it would work, but any other ideas that don't require interrupts or assembly would be appreciated.

    Thanks,

    Bart


    Main:

    delay=0
    button portb.1, 1, 254, 10, delay, 1, Held

    goto main

    Held:

    sound 0, 175, 25
    hold=0
    while hold < 5
    hold = hold + 1
    pause 10
    if portb.1 = 1 then button_held
    wend
    goto main

    button_held:

    sound 0, 175, 25
    pause 10
    sound 0, 175, 24

    ' do something else here

    goto main

    end

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


    Did you find this post helpful? Yes | No

    Default

    Ming thuang bing daaang zing YIIIHHHAAA
    Code:
        ' PIC12f629
        ' Push-button attach to GPIO<3:0> between i/o and VCC
        '
        ' it assume you'd previously disable all analog stuff and MCLR pin    
        '
        aPushButton var byte
        PushButton  var byte
        Delay       var Word
        
    Start:
          clear
          apushbutton=GPIO & $0F      ' read push-button
                                      ' keep only GPIO<3:0> bits
                                      '
          if apushbutton=0 then start ' no push button pressed, sit and spin
          pushbutton=apushbutton
          while (apushbutton!=0) 
                 apushbutton=GPIO & $0F
                 pause 10
                 delay=delay+1
                 wend
                 
          pause 50 ' debounce delay       
          if DELAY<100 then ' if push button is press for less than 1 second
             Select case PushButton
                    case 1
                         gosub PushButtonGPIO_0
                    case 2
                         gosub PushButtonGPIO_1
                    case 4
                         gosub PushButtonGPIO_2
                    case 8
                         gosub PushButtonGPIO_3
                    end select
             endif
          goto start
    The above should work. There's probably tons of different way to do it but, this is the first interrupt/Assembler/InternalRegister adressing free comming example.
    Last edited by mister_e; - 13th November 2005 at 20:22.
    Steve

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

  3. #3
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    Dude, I don't understand any of that! (well, a little bit, but mostly foggy to my mind).

    I gather you are reading the whole port, but I don't understand where CASE is getting any of those variables to distinguish the different buttons (if that's what it's doing).

    It looks like "while apushbutton!=0" is saying wait as long as the button is no longer pressed?

    Actually, is it doing what I described as the operation of the switch at all? It looks like it is doing something only if button is pressed and released under one second. Opposite than press once for one beep and press and hold for two beeps and another routine.

    For the record, the buttons are going to be on ports 0, 2, 4 and 5 so not straight across 0-3 which probably is easier, but not workable in what I have to do.

    Sorry, I'm not good enough at this to understand completely.

    Bart

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


    Did you find this post helpful? Yes | No

    Default

    OK, i missread the thing. Well at least you'd understand the core of the code. Now here's the modified version that will DO a routine if a button is pressed for more than TimeoutDelay on the pins 0, 2, 4 and 5. Kill me if i'm still wrong.. well try to
    Code:
        ' PIC12f629
        ' Push-button attach to GPIO 0, 2, 4 and 5 between i/o and VCC
        '
        ' it assume you'd previously disable all analog stuff and MCLR pin    
        '
        aPushButton  var byte
        PushButton   var byte
        Delay        var Word
        TimeoutDelay con 100  ' Here you select Your own delay (100=1 Second)
        
    Start:
          clear                        ' reset all variable to 0
          apushbutton=GPIO & %00110101 ' read all GPIO pins
                                       ' keep only GPIO 0,2,4,5 bits
                                       '     ----- HOW? -----
                                       '     By masking the unused bits with
                                       '     a bitwise AND
                                       '
          if apushbutton=0 then start  ' no push button pressed, sit and spin
          pushbutton=apushbutton       ' Store the push button press
                                       '
          while (apushbutton!=0) or (delay!=TimeoutDelay) ' Wait untill the timeout happen 
                                                          '    or push Button release
                 apushbutton=GPIO & %00110101
                 pause 10
                 delay=delay+1
                 wend
                 
          pause 50 ' debounce delay       
    
          if DELAY=timeoutdelay then ' if push button is press for 1 second
             '
             '    PushButton handling section
             '    ===========================
             '    We'd read the whole GPIO and set the un-assign bit to 0
             '    Push button are attach to GPIO 0,2,4,5.  Result is store in 
             '    PushButton variable
             '
             Select case PushButton
                    case %00000001
                         gosub PushButtonGPIO_0
                         
                    case %00000100
                         gosub PushButtonGPIO_2
                         
                    case %00010000
                         gosub PushButtonGPIO_4
                         
                    case %00100000
                         gosub PushButtonGPIO_5
                         
                    end select
             endif
          goto start
    Steve

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

  5. #5
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    Okay. I will need to think on this one. At lot of complex stuff happening compared to my little example. I wanted to take baby steps!

    Bart

  6. #6
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    Steve -

    Okay, after quite a bit of study here this is my take on your idea. Just to recap, the project is to work like this:

    User has four buttons
    Pressing a button produced a beep
    Continue to hold the button produces 2 beeps and the program jumps to appropriate routine.
    I've added an error in the event someone tries to press multiple combinations.

    I have a question about MCLR. You say that you assume it is disabled. I actually was wanting it enabled with the 4.7K resistor to the positive side. Does this create a problem while looking at that portb?

    Does this show some level of what you gave as an example and my opinion of it?

    Bart

    Code:
    getbutton	var	byte
    storebutton	var	byte
    delay 		var 	byte
    timeout		con	8			' adjust this value to set length of time to hold down button
    
    Start:
    
    	pause 15				' sandwich the button read between a couple pauses for debounce
    	getbutton = GPIO & %00110101		' get port values and keep only ports 0,2,4,5
    	pause 15
    
    	if getbutton = 0 then start		' sit and rotate if nothing pressed
    	
    	gosub beep				' jump and make a noise if button is pressed
    	storebutton = get button		' keep a copy of which button was pressed
    	delay = 0	
    			
    	while delay < timeout			' wait to see if button is being held down
    	pause 15				' sandwich the button read between a couple pauses for debounce
    	getbutton = GPIO & %00110101		' get port values again and keep only ports 0,2,4,5
    	pause 15
    	if getbutton <> storebutton then start	' bail out if user releases button too soon or switches buttons
    	delay = delay + 1
    	wend
    
    	gosub beep				' jump and make two beeps to acknowledge button was held down
    	pause 20
    	gosub beep 
    
    						' push and hold button section.  Jump to appropriate button
    	select case storebutton
    
    		case %00000001
    		goto button0
    
    		case %00000100
    		goto button2
    
    		case %00010000
    		goto button4
    
    		case %00100000
    		goto button5
    
    	end select
    
    	goto error				' jump to an error routine if someone did something unexpected
    					          like hold two buttons for example

Similar Threads

  1. Sony SIRC IR Issue
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th August 2015, 08:10
  2. Interruptus Frustratus
    By Byte_Butcher in forum General
    Replies: 16
    Last Post: - 17th April 2009, 20:36
  3. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  4. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43
  5. Button Push within 3 second Window
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd December 2005, 10:06

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